[BUG] Maps - MapHandler CallJSMethod throws System.InvalidOperationException
mikelor opened this issue · 0 comments
mikelor commented
Is there an existing issue for this?
- I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
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
- Compile and run the Samples/CommunityToolkit.Maui.Samples application on Windows
- 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.