From 80aba8c5b09d9d3d336805c33ae1a34c563d188a Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Tue, 3 Oct 2023 08:16:33 -0400 Subject: [PATCH 1/2] TryDispatchAsync for BlazorWebView 8.0 --- aspnetcore/blazor/hybrid/index.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/aspnetcore/blazor/hybrid/index.md b/aspnetcore/blazor/hybrid/index.md index ee86d9df1942..edf6f003d2e6 100644 --- a/aspnetcore/blazor/hybrid/index.md +++ b/aspnetcore/blazor/hybrid/index.md @@ -87,6 +87,34 @@ For more information, see the following resources: * [Xamarin.Forms String and Image Localization](/xamarin/xamarin-forms/app-fundamentals/localization/): The guidance generally applies to Blazor Hybrid apps. Not every scenario is supported at this time. * [Blazor Image component to display images that are not accessible through HTTP endpoints (dotnet/aspnetcore #25274)](https://github.com/dotnet/aspnetcore/issues/25274) +:::moniker range=">= aspnetcore-8.0" + +## Access scoped services from native UI + + + + has a `TryDispatchAsync` method that calls a specified `Action` asynchronously and passes in the scoped services available in Razor components. This enables code from the native UI to access scoped services such as : + +```csharp +private async void MyMauiButtonHandler(object sender, EventArgs e) +{ + var wasDispatchCalled = await _blazorWebView.TryDispatchAsync(sp => + { + var navMan = sp.GetRequiredService(); + navMan.CallSomeNavigationApi(...); + }); + + if (!wasDispatchCalled) + { + ... + } +} +``` + +When `wasDispatchCalled` is `false`, consider what to do if the call wasn't dispatched. The behavior is dependent on the app. + +:::moniker-end + ## Additional resources * From 7cb491f9150f19207fabb3d5e31f00ac056106e9 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Tue, 3 Oct 2023 09:18:48 -0400 Subject: [PATCH 2/2] Updates --- aspnetcore/blazor/hybrid/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/blazor/hybrid/index.md b/aspnetcore/blazor/hybrid/index.md index edf6f003d2e6..20ab1e1b5377 100644 --- a/aspnetcore/blazor/hybrid/index.md +++ b/aspnetcore/blazor/hybrid/index.md @@ -111,7 +111,7 @@ private async void MyMauiButtonHandler(object sender, EventArgs e) } ``` -When `wasDispatchCalled` is `false`, consider what to do if the call wasn't dispatched. The behavior is dependent on the app. +When `wasDispatchCalled` is `false`, consider what to do if the call wasn't dispatched. Generally, the dispatch shouldn't fail. If it fails, OS resources might be exhausted. If resources are exhausted, consider logging a message, throwing an exception, and perhaps alerting the user. :::moniker-end