-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why won't the WebView2 navigate when the Window is not in focus? #1845
Comments
Hey @michaldivis - I don't believe WebView2 should have a limitation here. If you put a breakpoint on setting the Source, can you confirm that the Source property is actually being set before the DialogWindow is closed? Which DialogWindow class is that, and when is the callback expected to be fired? |
Hey @champnic , I can confirm that the And to answer your second question, both the |
I've been messing with (but haven't changed any code) the app some more and now the app occasionally crashes after loading an item from the dialog window. This is the error I get in the Event Viewer:
Note that I've replaced the name of the actual app. |
Update: I've resolved the issue (kind of). My reproduction repo: https://github.com/michaldivis/WebView2DialogNavigation (warning: I couldn't reproduce the bug, but this repo might at least give you an idea of what my production app is like). The reproduction repo is very similar to what the actual app does, but for some reason, the bug just doesn't occur in the reproduction repo. After further testing, I might have figured out what the problem was. Here are two very similar methods to do the same thing, one works, the other doesn't: Method #1 - worksUsing this method, when item selected, the public ICommand OpenAddItemsCommand => new DarkAsyncCommand(OpenAddItemsInvokedByWpfControl); //this gets trigged by a Button control
private async Task OpenAddItemsInvokedByWpfControl()
{
await _navigationService.PushDialogAsync(AddItemAndUpdateContent); //passing in the AddItemAndUpdateContent method as a callback
}
private void AddItemAndUpdateContent(Item newItem)
{
HtmlContent = AddItemToHtml(HtmlContent, newItem);
} The Method #2 - does NOT workUsing this method, when item selected, the public ICommand LinkClickedCommand => new DarkAsyncCommand<string>(LinkClicked); //this gets triggered from the WebView2's NavigationStarting event (only if the url matches a specific pattern)
private async Task LinkClicked(string link)
{
if (string.IsNullOrEmpty(link))
{
return;
}
var addItemPattern = Regex.Match(link, @"ADD_ITEM");
if (addItemPattern.Success)
{
await _navigationService.PushDialogAsync(AddItemAndUpdateContent); //passing in the AddItemAndUpdateContent method as a callback
}
} Here the private void WebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
string linkName = Path.GetFileName(e.Uri);
var isTempFileName = _fileManager.IsTempFilePath(linkName);
if (isTempFileName)
{
return;
}
e.Cancel = true;
TriggerLinkClicked(linkName);
}
private void TriggerLinkClicked(string link)
{
bool canExecute = LinkClickedCommand?.CanExecute(link) ?? false;
if (canExecute is false)
{
return;
}
LinkClickedCommand?.Execute(link);
} My conclusionI still have no idea why this is happening and as I've metioned, I'm having trouble even replicating the issue so don't take this as a fault of the My only guess is that invoking the I'll appreciate any help here. Thanks! |
Hey @michaldivis - thanks for the extra digging and details. The cause might be that in the case where it doesn't work, you are doing it from within a NavigationStarting event handler. This will block the WebView2 from doing further navigations, because it's waiting for the outcome of the first event handler (for example, the WebView2 needs to know whether or not to cancel the navigation). The easiest workaround is to make Thanks! |
Hey @champnic , that's it! Thanks! The If I open the dialog window normally (using Thanks for all the help! |
Glad you got it working @michaldivis! :) |
Hey, thanks! I have a similar issue with OpenFileDialog where if I navigate away from the BlazorWebView it crashes the dialog. But changing the method to async works! |
Hi,
I have a question / maybe a bug report. This is what's happening to me:
I have a
WebView2
on aWindow
, let's call it mainWindow. I open anotherWindow
, a dialogWindow. The dialogWindow invokes a callback to the mainWindow to update the HTML file loaded into theWebView2
. However, even after setting theWebView2
'sSource
property, no navigation will happen until the dialogWindow is closed. Only then is theNavigationStarting
event triggered.Is that supposed to be the case? An if so, is there a way I can get the
WebView2
to update while the dialogWindow is open? Thanks for any advice!Here's more information about my app:
It's a .NET 5.0 WPF app and I'm using Microsoft.Web.WebView2 version 1.0.902.49.
My mainWindow, that holds the
WebView2
control opens the dialogWindow like this, passing in a callback function to refresh the HTML displayed in theWebView2
:The text was updated successfully, but these errors were encountered: