How to disconnect print event handler in uwp printing?

a 夏天 提交于 2021-01-29 21:52:21

问题


I need to print a single xaml-page within a multiple page UWP-app. I only get as far as printing this page, but when leaving that page and trying to navigate back to it the following exception is thrown:

For the PrintTaskRequested-event only one handler at a time can be registered (translated).

The MS-instruction says that you have to disconnect the printing event handlers when you leave the printing page. Strange enough I found 2 short and understandable uwp-printing examples here on Stackoverflow which are marked as answers, but lacked a method to disconnect, so both broke like mine when adding a second page.

The MS Print Sample is too complex for me as a beginner. I tried to build my code using pieces from the Sample code, but I got lost in errors.

This is my testing code so far. I removed every piece that is not needed - just to preview and print a page:

   using Microsoft.Toolkit.Uwp.Helpers;

namespace Print_190905
{
public sealed partial class MainPage : Page
{
    private PrintManager printMan;
    private PrintDocument printDoc;
    private IPrintDocumentSource printDocSource;
    private PrintHelper printHelper;

    public MainPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        printMan = PrintManager.GetForCurrentView();
        printMan.PrintTaskRequested += PrintTaskRequested;
        printDoc = new PrintDocument();
        printDocSource = printDoc.DocumentSource;
        printDoc.GetPreviewPage += GetPreviewPage;
    }


    private async void PrintButton_Click(object sender, RoutedEventArgs e)
    {
                await PrintManager.ShowPrintUIAsync();
    }

    private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
    {
        var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequrested);
    }

    private void PrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
    {
        args.SetSource(printDocSource);
    }

    private void GetPreviewPage(object sender, GetPreviewPageEventArgs e)
    {
        printDoc.SetPreviewPage(e.PageNumber, this.Print_Area);
    }

    private void CmdZurueck_Click(object sender, RoutedEventArgs e)
    {
        this.Frame.Navigate(typeof(Seite_2));
    }

}

To avoid the error when returning to the print-page, I added this piece from the Print Sample:

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        if (printHelper != null)
        {
            printHelper.Dispose();
        }
    }

But: printHelper is always Null, so the “Dispose”-method is never called. There is no place in the code where a value is assigned, and I could not find out where in the Print Sample the value for printHelper is assigned.

When I set a breakpoint in the PrintSample, the value is “(PrintSample.PrintHelper)”.

What does that mean? Can I assign a valid value to printHelper somehow? It took me many hours to get that far. Therefore I would be very grateful if somebody could help! Thanks!


回答1:


printMan.PrintTaskRequested -= PrintTaskRequested;

and

printDoc.GetPreviewPage -= GetPreviewPage;

within OnNavigatedFrom.

The above functions are re-registered each time you enter the page. However it does not get unregistered when leaving the page. So the more you enter the page, the more functions you have registered.



来源:https://stackoverflow.com/questions/57834268/how-to-disconnect-print-event-handler-in-uwp-printing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!