Windows 打印操作期间存储应用程序错误

Windows store app error during print operation

我是 运行 我的 windows 商店应用程序,我遇到了这样的错误。

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code

WinRT information: Only one handler for the PrintTaskRequested event may be registered at a time.

Additional information: A method was called at an unexpected time.

我的代码是 here.help 我了解问题并解决这个问题 issue.Kindly 告诉我确切的问题。

//print sample
    protected PrintDocument printDocument = null;
    protected IPrintDocumentSource printDocumentSource = null;
    internal List<UIElement> printPreviewElements = new List<UIElement>();
    protected event EventHandler pagesCreated;

打印任务请求处理程序出错

    protected virtual void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
    {
        PrintTask printTask = null;
        printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>
        {
            printTask.Completed += async (s, args) =>
            {
                if (args.Completion == PrintTaskCompletion.Failed)
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                    {
                        MessageDialog dialog = new MessageDialog("Something went wrong while trying to print. Please try again.");
                        await dialog.ShowAsync();
                    });
                }
            };
            sourceRequested.SetSource(printDocumentSource);
        });
    }

    protected virtual  void RegisterForPrinting()
    {
        printDocument = new PrintDocument();
        printDocumentSource = printDocument.DocumentSource;
        printDocument.Paginate += CreatePrintPreviewPages;
        printDocument.GetPreviewPage += GetPrintPreviewPage;
        printDocument.AddPages += AddPrintPages;
        PrintManager printMan = PrintManager.GetForCurrentView();
        printMan.PrintTaskRequested += PrintTaskRequested;
    }

    protected virtual void UnregisterForPrinting()
    {
        if (printDocument != null)
        {
            printDocument.Paginate -= CreatePrintPreviewPages;
            printDocument.GetPreviewPage -= GetPrintPreviewPage;
            printDocument.AddPages -= AddPrintPages;
            PrintManager printMan = PrintManager.GetForCurrentView();
            printMan.PrintTaskRequested -= PrintTaskRequested;
        }
    }

    protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
    {
        printPreviewElements.Clear();
        PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
        PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
        AddOnePrintPreviewPage(pageDescription);
        if (pagesCreated != null)
        {
            pagesCreated.Invoke(printPreviewElements, null);
        }
        ((PrintDocument)sender).SetPreviewPageCount(printPreviewElements.Count, PreviewPageCountType.Intermediate);
    }

    protected virtual void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
    {
        ((PrintDocument)sender).SetPreviewPage(e.PageNumber, printPreviewElements[e.PageNumber - 1]);
    }

    protected virtual void AddPrintPages(object sender, AddPagesEventArgs e)
    {
        foreach (UIElement element in printPreviewElements)
        {
            printDocument.AddPage(element);
        }
        ((PrintDocument)sender).AddPagesComplete();
    }

    protected virtual void AddOnePrintPreviewPage(PrintPageDescription printPageDescription)
    {
        TextBlock block = new TextBlock();
        block.Text = "This is an example.";
        block.Width = printPageDescription.PageSize.Width;
        block.Height = printPageDescription.PageSize.Height;
        printPreviewElements.Add(block);
    }
    //protected override void OnNavigatedTo(NavigationEventArgs e)
    //{
    //    RegisterForPrinting();
    //}

    //protected override void OnNavigatedFrom(NavigationEventArgs e)
    //{
    //    UnregisterForPrinting();
    //}

    private void printBirth_Click(object sender, RoutedEventArgs e)
    {
        RegisterForPrinting();
    }

你可以使用它

private async void printBirth_Click(object sender, RoutedEventArgs e)
    {
        await Windows.Graphics.Printing.PrintManager.showPrintUIAsync()
    }