如何在 uwp 打印中断开打印事件处理程序? [英] How to disconnect print event handler in uwp printing?

查看:52
本文介绍了如何在 uwp 打印中断开打印事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在多页 UWP 应用中打印单个 xaml 页面.我只能打印此页面,但是当离开该页面并尝试导航回该页面时,会引发以下异常:

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:

对于 PrintTaskRequested 事件,一次只能注册一个处理程序(翻译).

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

MS 指令说您必须在离开打印页面时断开打印事件处理程序的连接.奇怪的是,我在 Stackoverflow 上发现了 2 个简短易懂的 uwp 打印示例,它们被标记为答案,但缺少断开连接的方法,因此在添加第二页时两者都像我一样坏了.

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.

MS 打印示例对我这个初学者来说太复杂了.我尝试使用示例代码中的片段构建我的代码,但我迷失在错误中.

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();
        }
    }

但是:printHelper 始终为 Null,因此永远不会调用Dispose"方法.代码中没有分配值的地方,我无法找到打印示例中printHelper 的值分配的位置.

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.

当我在 PrintSample 中设置断点时,值为(PrintSample.PrintHelper)".

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

这是什么意思?我可以以某种方式为 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!

推荐答案

printMan.PrintTaskRequested -= PrintTaskRequested;

printDoc.GetPreviewPage -= GetPreviewPage;

在 OnNavigatedFrom 中.

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.

这篇关于如何在 uwp 打印中断开打印事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆