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

时间:2015-11-16 09:15:42

标签: c# exception printing windows-8

我正在运行我的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();
    }

1 个答案:

答案 0 :(得分:1)

你可以使用它

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