Windows store app error during print operation

prathik

I am running my windows store app and i got error like this.

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.

My code is here.help me to get understand the problem and resolve this issue.Kindly tell me know the exact problem.

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

Error came in print task requested handler

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

you can use it

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Local file storage during development of a Windows Store App

Iam getting iTunes store operation failed error when validating the app

App Store Connect Operation Error: Missing CFBundleIdentifier in path

Xcode 12 Beta 3: App Store Connect Operation Error

'Error occurred during a cryptographic operation' during local IIS deployment of ASP.NET MVC App

Registration of the app failed error in Windows Store or Universal app

Activation of the Windows Store app ... failed with error 'The app didn't start'

Error during UWP app launch in Windows 10 mobile

How to fix App Store Connect Operation Error ERROR ITMS-90784

How to deal with: App Store Connect Operation Error (ERROR ITMS-90744: "Unsupported Xcode or SDK Version.)

xcode 6 upload app with error: iTunes Store operation failed Error Description not available

Error during installation of Windows

Error when using the WebView control in Windows Store App

Windows Store App not Scaling

Slider in Windows Store App

Windows store app update

Windows Store App Development

App Store Connect Operation Error: ITMS-90164 [] for key com.apple.developer.healthkit.access

Distribute content button during uploading app store

Windows Store app is currently not available in Windows Store

Error occurred during a cryptographic operation in debug

@ngrx/store Error on production build : Error during template compile of 'Store'

The Invincible error with Windows Store

VBA error 1004 during print PDF

Error during the print of nodes in a python binary tree

Syntactic error during the print of a class properties

Windows Store Cordova App + WinJS: app crashes with a JavaScript runtime error on launch

Windows Phone 8.1 Store app - Link to store

iTunes Store Operation failed: Error description not available