一次只能为 PrintTaskRequested 事件注册一个处理程序 UWP 打印错误
Only one handler for the PrintTaskRequested event may be registered at a time UWP print error
在 Mainpage
构造函数中,我有这一行:
PrintManager.GetForCurrentView().PrintTaskRequested += OnPrintTaskRequested;
我在菜单栏中有一个按钮,用于使用此单击事件处理程序进行打印:
CanvasPrintDocument printDocument;
public async void PrintButtonClick()
{
if (printDocument != null)
{
printDocument.Dispose();
}
printDocument = new CanvasPrintDocument();
printDocument.Preview += (sender, args) =>
{
sender.SetPageCount(1);
PrintPage(args.DrawingSession, args.PrintTaskOptions.GetPageDescription(1));
};
printDocument.Print += (sender, args) =>
{
using var ds = args.CreateDrawingSession();
PrintPage(ds, args.PrintTaskOptions.GetPageDescription(1));
};
try
{
await PrintManager.ShowPrintUIAsync();
}
catch (Exception)
{
}
}
private void PrintPage(CanvasDrawingSession ds, PrintPageDescription printPageDescription)
{
//
}
此按钮还有键盘快捷键 CTRL + P。
现在,这适用于我的机器,在调试和发布模式下,没问题。
但是,我收到了一些关于我的应用程序的崩溃报告,其中显示错误:
A method was called at an unexpected time.
Only one handler for the PrintTaskRequested event may be registered at
a time.
我不明白 PrintTaskRequested
事件是如何一次注册多次的。由于事件处理程序注册代码在构造函数中。
我的应用程序中没有任何其他打印逻辑。
感谢任何帮助。谢谢。
如果事件注册发生在MainPage的构造函数中,您可以检查应用程序是否可以多次导航到MainPage。
但更常见的方法是在注册之前取消事件。
PrintManager.GetForCurrentView().PrintTaskRequested -= OnPrintTaskRequested;
PrintManager.GetForCurrentView().PrintTaskRequested += OnPrintTaskRequested;
这确保始终只注册一个打印事件。
谢谢。
在 Mainpage
构造函数中,我有这一行:
PrintManager.GetForCurrentView().PrintTaskRequested += OnPrintTaskRequested;
我在菜单栏中有一个按钮,用于使用此单击事件处理程序进行打印:
CanvasPrintDocument printDocument;
public async void PrintButtonClick()
{
if (printDocument != null)
{
printDocument.Dispose();
}
printDocument = new CanvasPrintDocument();
printDocument.Preview += (sender, args) =>
{
sender.SetPageCount(1);
PrintPage(args.DrawingSession, args.PrintTaskOptions.GetPageDescription(1));
};
printDocument.Print += (sender, args) =>
{
using var ds = args.CreateDrawingSession();
PrintPage(ds, args.PrintTaskOptions.GetPageDescription(1));
};
try
{
await PrintManager.ShowPrintUIAsync();
}
catch (Exception)
{
}
}
private void PrintPage(CanvasDrawingSession ds, PrintPageDescription printPageDescription)
{
//
}
此按钮还有键盘快捷键 CTRL + P。
现在,这适用于我的机器,在调试和发布模式下,没问题。
但是,我收到了一些关于我的应用程序的崩溃报告,其中显示错误:
A method was called at an unexpected time.
Only one handler for the PrintTaskRequested event may be registered at a time.
我不明白 PrintTaskRequested
事件是如何一次注册多次的。由于事件处理程序注册代码在构造函数中。
我的应用程序中没有任何其他打印逻辑。
感谢任何帮助。谢谢。
如果事件注册发生在MainPage的构造函数中,您可以检查应用程序是否可以多次导航到MainPage。
但更常见的方法是在注册之前取消事件。
PrintManager.GetForCurrentView().PrintTaskRequested -= OnPrintTaskRequested;
PrintManager.GetForCurrentView().PrintTaskRequested += OnPrintTaskRequested;
这确保始终只注册一个打印事件。
谢谢。