Skip to content
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

Fix for #2407 - MapHandler Throws InvalidSystemOperationExcetption #2409

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ static async Task CallJSMethod(FrameworkElement platformWebView, string script)
{
if (platformWebView is WebView2 webView2)
{
await webView2.EnsureCoreWebView2Async();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only need to call it once. lets move it to CreatePlatformView

Copy link
Contributor Author

@mikelor mikelor Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @VladislavAntonyuk
The method should be awaited.

Moving to CreatePlatformView would require modification of that method to async.
Was unsure of side effects.

In the remarks for the call....
https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.wpf.webview2.ensurecorewebview2async

there is no issue calling this mutiple times, and potentially a change in the view might need to call this again, therefore I placed it here.

Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no issue calling EnsureCoreWebView2Async multiple times, I'm cool with keeping it in CallJSMethod.

We could call EnsureCoreWebView2Async in CreatePlatformView(), but we'd need to include a fire-and-forget async void, and I prefer to avoid this given that calling EnsureCoreWebView2Async from CallJSMethod works.

Just for fun, here's how we would modify CreatePlatformView:

	protected override FrameworkElement CreatePlatformView()
	{
		if (string.IsNullOrEmpty(MapsKey))
		{
			throw new InvalidOperationException("You need to specify a Bing Maps Key");
		}

		var mapPage = GetMapHtmlPage(MapsKey);
		var webView = new MauiWebView(new WebViewHandler());
		webView.NavigationCompleted += HandleWebViewNavigationCompleted;
		webView.WebMessageReceived += WebViewWebMessageReceived;
		webView.LoadHtml(mapPage, null);

		InitializeMap(webView);

		return webView;

		// Use async-void to properly use async/await to initialize the map from a non-async method
		static async void InitializeMap(MauiWebView webView2)
		{
			await webView2.EnsureCoreWebView2Async();
		}
	}


var tcs = new TaskCompletionSource();
webView2.DispatcherQueue.TryEnqueue(async () =>
{
Expand Down