WinUI 3.0 - UWP: Error on WebView2 Print functionality

最后都变了- 提交于 2020-07-23 06:24:06

问题


In my WinUI 3.0 - UWP project, I have a WebView2 control that displays a simple html as follows. But when I call the following javascript code using ExecuteScriptAsync (as shown below), I get the following error:

Error:

Check your printer or select another printer. The printer you chose isn't available or isn't installed correctly.

Screenshot of Error:

Remarks: There is nothing wrong on my Windows 10's default Print to PDF printer as I can print the exact same html page from a browser (MS Edge or Google).

Question: What I may be doing wrong here, and can we resolve the issue? I think a cause of the issue may have something to do with how I'm using the JavaScript in my html and/or how I'm passing a parameter to the ExecuteScriptAsync(...) method. Note: wvTest, as you may have guessed, is the name of WebView2 control.

Code:

//Correctly displays the html page with a simple text: `Test paragraph`
private async void myButton_Click(object sender, RoutedEventArgs e)
{
    string sHTML = "<!DOCTYPE html>" +
        "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">" +
        "<head>" +
            "<meta charset=\"utf-8\" />" +
            "<title>Test Title</title>" +
        "</head>" +
        "<body>" +
            "<p>Test paragraph</p>" +
            "<script> window.print();</script>" +
        "</body>" +
        "</html>";
    //System.Diagnostics.Debug.WriteLine(sHTML);
    wvTest.NavigateToString(sHTML);
}

//This event opens the Print Dialog with the message shown above:

private async void btnPrint_Click(object sender, RoutedEventArgs e)
{
    await wvTest.ExecuteScriptAsync(javascriptCode: "window.print();");
}

来源:https://stackoverflow.com/questions/62869094/winui-3-0-uwp-error-on-webview2-print-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!