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

TryDispatchAsync for BlazorWebView 8.0 #30556

Merged
merged 2 commits into from
Oct 3, 2023
Merged
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
28 changes: 28 additions & 0 deletions aspnetcore/blazor/hybrid/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- UPDATE 8.0 Cross-link API -->

<xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> has a `TryDispatchAsync` method that calls a specified `Action<ServiceProvider>` asynchronously and passes in the scoped services available in Razor components. This enables code from the native UI to access scoped services such as <xref:Microsoft.AspNetCore.Components.NavigationManager>:

```csharp
private async void MyMauiButtonHandler(object sender, EventArgs e)
{
var wasDispatchCalled = await _blazorWebView.TryDispatchAsync(sp =>
{
var navMan = sp.GetRequiredService<NavigationManager>();
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.
guardrex marked this conversation as resolved.
Show resolved Hide resolved

:::moniker-end

## Additional resources

* <xref:blazor/hybrid/tutorials/index>
Expand Down