我有一个 .net Framework v4.7.2 WPF 应用程序打印了一个 FixedPage
,它在 Windows 7 上运行时没有任何问题,但在 Windows 8.1 上运行时我遇到了一些问题.
以下代码块是造成问题的原因:
private void Print()
{
using (var printQueue = LocalPrintServer.GetDefaultPrintQueue())
{
var printTicket = printQueue.DefaultPrintTicket;
var writer = PrintQueue.CreateXpsDocumentWriter(printQueue);
writer.WritingCompleted += OnDocumentWritten;
writer.WriteAsync(View.LabelPageView.AddressLabelPage, printTicket);
}
}
它会抛出一个 NullReferenceException
,堆栈跟踪如下:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Hashtable.get_Item(Object key)
at System.Printing.PrintQueue.get_IsXpsDevice()
at System.Printing.PrintQueue.ForwardXpsDriverDocEvent(Object sender, XpsSerializationXpsDriverDocEventArgs args)
at System.Windows.Xps.Serialization.XpsSerializationManager.OnXpsDriverDocEvent(XpsSerializationXpsDriverDocEventArgs e)
at System.Windows.Xps.Serialization.XpsDriverDocEventManager.OnXpsDriverDocEvent()
at System.Windows.Xps.Serialization.XpsDriverDocEventManager.ForwardPackagingProgressEvent(Object sender, PackagingProgressEventArgs e)
at System.Windows.Xps.Packaging.PackagingProgressEventHandler.Invoke(Object sender, PackagingProgressEventArgs e)
at System.Windows.Xps.Packaging.XpsInterleavingPolicy.AddItem(INode n, Int32 number, INode parent)
at System.Windows.Xps.Serialization.XpsPackagingPolicy.AcquireXmlWriterForFixedPage()
at System.Windows.Xps.Serialization.XpsSerializationManager.AcquireXmlWriter(Type writerType)
at System.Windows.Xps.Serialization.FixedPageSerializerAsync.get_XmlWriter()
at System.Windows.Xps.Serialization.ReachTreeWalker..ctor(ReachSerializer serializer)
at System.Windows.Xps.Serialization.FixedPageSerializerAsync.PersistObjectData(SerializableObjectContext serializableObjectContext)
at System.Windows.Xps.Serialization.ReachSerializerAsync.BeginSerializeObject(Object serializedObject)
at System.Windows.Xps.Serialization.FixedPageSerializerAsync.SerializeObject(Object serializedObject)
at System.Windows.Xps.Serialization.XpsSerializationManagerAsync.InvokeSaveAsXamlWorkItem(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
如果我将 writer.WriteAsync(...)
替换为 writer.Write(...)
,错误就会消失。
我已经能够在 Windows 8.1、Windows Server 2012 R2 和 Windows Server 2016 上重现该错误。我无法在 Windows 10 上重现该错误。
关于如何在 Windows 8.1 上异步打印有什么建议吗?
最佳答案
您可以使方法 async
然后 await
异步调用。
试试这个:
private async Task Print()
{
using (var printQueue = LocalPrintServer.GetDefaultPrintQueue())
{
var printTicket = printQueue.DefaultPrintTicket;
var writer = PrintQueue.CreateXpsDocumentWriter(printQueue);
writer.WritingCompleted += OnDocumentWritten;
await writer.WriteAsync(View.LabelPageView.AddressLabelPage, printTicket);
}
}
关于c# - 在 Windows 8.1 中打印 WPF FixedPage 时为空引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55302872/