问题
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