打印操作期间出现Windows商店应用程序错误

本文关键字:应用程序 错误 Windows 操作 打印 | 更新日期: 2023-09-27 18:22:00

我正在运行我的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.

我的代码在这里。请帮助我理解问题并解决此问题。请告诉我确切的问题。

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

打印操作期间出现Windows商店应用程序错误

您可以使用它

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