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

[BUG] Maps - MapHandler CallJSMethod throws System.InvalidOperationException #2407

Open
2 tasks done
mikelor opened this issue Dec 20, 2024 · 0 comments · May be fixed by #2409
Open
2 tasks done

[BUG] Maps - MapHandler CallJSMethod throws System.InvalidOperationException #2407

mikelor opened this issue Dec 20, 2024 · 0 comments · May be fixed by #2409
Labels
bug Something isn't working unverified

Comments

@mikelor
Copy link
Contributor

mikelor commented Dec 20, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

When running the CommunityToolkit.Maui.Sample application and selecting the Views / Map Basic Page, a System.InvalidOperationException is thrown.

Expected Behavior

The Views / Map Basic Page should be displayed, showing the Map View.

Steps To Reproduce

  1. Compile and run the Samples/CommunityToolkit.Maui.Samples application on Windows
  2. Select Views / Maps Basic Page

Link to public reproduction project repository

https://github.com/CommunityToolkit/Maui/

Environment

- .NET MAUI CommunityToolkit: 10.0.0
- OS: Windows 11 Pro 24H2
- .NET MAUI: 9.0.101

Anything else?

You do not need to specify a Bing Maps key to observe the error.

The exception thrown is

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
WinRT information: ExecuteScriptAsync(): Failed because a valid CoreWebView2 is not present. Make sure one was created, for example by calling EnsureCoreWebView2Async() API.
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code
WinRT information: ExecuteScriptAsync(): Failed because a valid CoreWebView2 is not present. Make sure one was created, for example by calling EnsureCoreWebView2Async() API.
A method was called at an unexpected time.

To fix this, I believe the CallJSMethod should be modified to include a call to webView2.EnsureCoreWebView2Async() before attempting to call a JasvaScript method. As shown below:

	static async Task CallJSMethod(FrameworkElement platformWebView, string script)
	{
		if (platformWebView is WebView2 webView2)
		{
+                     	await webView2.EnsureCoreWebView2Async();

			var tcs = new TaskCompletionSource();
			webView2.DispatcherQueue.TryEnqueue(async () =>
			{
				await webView2.ExecuteScriptAsync(script);
				tcs.SetResult();
			});

			await tcs.Task;
		}
	}

I will submit a PR in the next day or so, with the supplied fix.

@mikelor mikelor added bug Something isn't working unverified labels Dec 20, 2024
mikelor added a commit to mikelor/CommunityToolkit-Maui that referenced this issue Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unverified
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant