I have the following function to save a report to a xps file:
public XpsDocumentWriter Save(string fileName, Action<string> onSaveCompletion, Action<string> onSaveError)
{
string xpsFileName = Path.GetTempFileName();
xpsFileName = Path.ChangeExtension(xpsFileName, "xps");
// make sure we delete any existing files
File.Delete(xpsFileName);
File.Delete(fileName);
XpsDocument savedDocument = new XpsDocument(xpsFileName, FileAccess.ReadWrite);
XpsDocumentWriter documentWriter = XpsDocument.CreateXpsDocumentWriter(savedDocument);
if (documentWriter != null)
{
WritingCompletedEventHandler handler = null;
handler = (sender, e) => {
DocumentWriter_WritingCompleted(xpsFileName, fileName, savedDocument, onSaveCompletion, onSaveError, e);
documentWriter.WritingCompleted -= handler;
};
documentWriter.WritingCompleted += handler;
documentWriter.WriteAsync(FixedDocument);
}
return documentWriter;
}
When the caller is in a windows application, everything works fine. But when the caller is in a class library, nothing is write into the xps file and no WritingCompletedEvent is triggered. I also tried WritingProgressChangedEvent and it was not triggered either. In the case of class library, synchronous call documentWriter.Write(FixedDocument) works. But I prefer to use asynchronous call since I may have a big report to save or print. Anybody have any idea how to make asynchronous call WriteAsync work with the class library? Thanks!!!