Reputation: 54
I have a WPF app that uses webview2. When users navigate to a certain URL, there are links to pdfs that will open in a new window. The problem I am trying to address is that the new windows don't respect the sizing of the browser (a known issue with wv2).
Based on what I am reading, I can address this by setting the NewWindow property on the CoreWebView2NewWindowRequestedEventArgs to my existing webview2's CoreWebView2, but when I do that, it:
Version Version: WebView2 1.0.902.49 .NET Framework 4.7.2 OS Wind10
Additional notes:
Even when I comment out all the above code and throw a debugger in that method to inspect the event args, it still overwrites the existing window (though all code is commented out). However, it only seems to do this when I hit the debugger and if I remove it, the window remains and the popup opens.
When I inspect the uri given, it is "about:blank#blocked", which is likely why the page is blank. So if I can't get the actual uri / url for this pdf, I can't open it in the new window even if I could get this to work.
Upvotes: 0
Views: 1642
Reputation: 483
I created a generic container for WebView2, and allow the application users - developers and analysts deploying it in their systems - to define how they would like to handle new windows, i.e., open in same web view, open in new web view, open in default browser, or block.
private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
switch (viewModel.Configuration.NewWindow)
{
case SharedComponents.BrowserWindow.NewWindowOption.BLOCK:
e.Handled = true;
break;
case SharedComponents.BrowserWindow.NewWindowOption.OPEN:
e.Handled = false;
break;
case SharedComponents.BrowserWindow.NewWindowOption.OPENNEW:
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string workingFolder = System.IO.Directory.GetCurrentDirectory();
viewModel.StartNewWebViewer(exePath, workingFolder, e.Uri);
e.Handled = true;
break;
case SharedComponents.BrowserWindow.NewWindowOption.OPENSAME:
e.Handled = true;
this.webView.Source = new Uri(e.Uri);
break;
default:
e.Handled = true;
break;
}
}
The code for launching the new window looks like this, where I use the defining parameters of the app, along with the new URL, to launch a new window of the base app, and that hosts its own WebVeiw2:
Public Sub StartNewWebViewer(exePath As String, workingFolder As String, newPath As String) Implements IViewModel.StartNewWebViewer
Dim processString = If(Configuration.TitleVisibility = Visibility.Visible, "", "-hideTitle ")
processString &= If(Configuration.DisplayMskIcon = Visibility.Visible, "-showIcon ", "")
processString &= If(Configuration.ResizeWindow = True, "", "-noResize ")
processString = String.Format("{0} -nw {1} -u ""{2}"" -t ""{3}"" -g {4} -l {5}", processString, Configuration.NewWindow.ToString(), newPath, Configuration.Title, ParentGuid, loggingName)
Dim newProcessStartInfo = New ProcessStartInfo()
With newProcessStartInfo
.FileName = exePath
.Arguments = processString
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardOutput = False
.RedirectStandardInput = False
.ErrorDialog = True
.Verb = "runas"
.WindowStyle = ProcessWindowStyle.Normal
.WorkingDirectory = workingFolder
End With
Using newProcess As New Process()
With newProcess
.StartInfo = newProcessStartInfo
.Start()
End With
End Using
End Sub
Upvotes: 0
Reputation: 4377
You use the CoreWebView2NewWindowRequestedEventArgs.NewWindow
property to tell WebView2 where to show the new window. If you set NewWindow to the CoreWebView2 that fired the event, then the new window will take over the existing CoreWebView2.
You'll need to create your own new window (or whatever UI is appropriate) containing a new WebView2 and set the CoreWebView2NewWindowRequestedEventArgs.NewWindow
property to that new CoreWebView2 if you want to have control over a new window with a new WebView2.
Upvotes: 0