Skip to content

Commit bc9fe76

Browse files
authored
MAUI Blazor Hybrid + BWA article 9.0 updates (#33865)
1 parent 3262acd commit bc9fe76

File tree

1 file changed

+61
-67
lines changed

1 file changed

+61
-67
lines changed

aspnetcore/blazor/hybrid/tutorials/maui-blazor-web-app.md

Lines changed: 61 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ description: Learn how to build a .NET MAUI Blazor Hybrid app with a Blazor Web
55
monikerRange: '>= aspnetcore-8.0'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 06/20/2024
8+
ms.date: 10/17/2024
99
uid: blazor/hybrid/tutorials/maui-blazor-web-app
1010
---
1111
# Build a .NET MAUI Blazor Hybrid app with a Blazor Web App
1212

13-
<!-- DOC AUTHOR NOTE - Refactor the @-prefixed component param values
14-
when the 8.0 sample changes naming to match
15-
the 9.0 template-produced sample on
16-
https://github.com/dotnet/AspNetCore.Docs/issues/32802. -->
17-
1813
This article shows you how to build a .NET MAUI Blazor Hybrid app with a Blazor Web App that uses a shared user interface via a Razor class library (RCL).
1914

2015
## Prerequisites and preliminary steps
@@ -73,8 +68,7 @@ Add new project to the solution with the **Blazor Web App** project template. Se
7368
* **Interactivity location**: **Global**
7469
* **Sample pages**: Unselected (disabled)
7570

76-
<!-- UPDATE 9.0 Check on PU issue mentioned below and
77-
revise accordingly. -->
71+
<!-- UPDATE 10.0 Check on PU issue mentioned below and revise accordingly. -->
7872

7973
The **Interactivity location** setting to **Global** is important because MAUI apps always run interactively and throw errors on Razor component pages that explicitly specify a render mode. If you don't use a global render mode, you must implement the approach described in the [Use Blazor render modes](#use-blazor-render-modes) section after following the guidance in this section. For more information, see [BlazorWebView needs a way to enable overriding ResolveComponentForRenderMode (`dotnet/aspnetcore` #51235)](https://github.com/dotnet/aspnetcore/issues/51235).
8074

@@ -299,22 +293,23 @@ For the Blazor Web App on the web client, the property values are assigned from
299293

300294
`InteractiveRenderSettings.cs`:
301295

302-
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Shared/InteractiveRenderSettings.cs":::
296+
:::code language="csharp" source="~/../blazor-samples/9.0/MauiBlazorWeb/MauiBlazorWeb.Shared/InteractiveRenderSettings.cs":::
303297

304298
In `MauiProgram.CreateMauiApp` of `MauiProgram.cs`, call `ConfigureBlazorHybridRenderModes`:
305299

306300
```csharp
307301
InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();
308302
```
309303

310-
In the RCL's `_Imports.razor` file, add the following global static `@using` directive to make the properties of the class available to components:
304+
In the `_Imports.razor` file of the `.Shared` RCL, replace the `@using` statement for <xref:Microsoft.AspNetCore.Components.Web.RenderMode?displayProperty=fullName> with an `@using` statement for `InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
311305

312-
```razor
313-
@using static InteractiveRenderSettings
306+
```diff
307+
- @using static Microsoft.AspNetCore.Components.Web.RenderMode
308+
+ @using static InteractiveRenderSettings
314309
```
315310

316311
> [!NOTE]
317-
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> via the `@using static Microsoft.AspNetCore.Components.Web.RenderMode` statement in the Blazor Web App's `_Import` file.
312+
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> in the Blazor Web App's `_Import` file.
318313
319314
### Per-page/component Auto interactivity
320315

@@ -340,22 +335,23 @@ For the Blazor Web App on the web client, the property values are assigned from
340335

341336
`InteractiveRenderSettings.cs`:
342337

343-
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Shared/InteractiveRenderSettings.cs":::
338+
:::code language="csharp" source="~/../blazor-samples/9.0/MauiBlazorWeb/MauiBlazorWeb.Shared/InteractiveRenderSettings.cs":::
344339

345340
In `MauiProgram.CreateMauiApp` of `MauiProgram.cs`, call `ConfigureBlazorHybridRenderModes`:
346341

347342
```csharp
348343
InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();
349344
```
350345

351-
In the RCL's `_Imports.razor` file, add the following global static `@using` directive to make the properties of the class available to components:
346+
In the `_Imports.razor` file of the `.Shared.Client` RCL, replace the `@using` statement for <xref:Microsoft.AspNetCore.Components.Web.RenderMode?displayProperty=fullName> with an `@using` statement for `InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
352347

353-
```razor
354-
@using static InteractiveRenderSettings
348+
```diff
349+
- @using static Microsoft.AspNetCore.Components.Web.RenderMode
350+
+ @using static InteractiveRenderSettings
355351
```
356352

357353
> [!NOTE]
358-
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> via the `@using static Microsoft.AspNetCore.Components.Web.RenderMode` statement in the Blazor Web App's `_Import` file.
354+
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> in the Blazor Web App's `_Import` file.
359355
360356
### Per-page/component WebAssembly interactivity
361357

@@ -405,7 +401,7 @@ For the Blazor Web App on the web client, the property values are assigned from
405401

406402
`InteractiveRenderSettings.cs` (`.Shared.Client` RCL):
407403

408-
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Shared/InteractiveRenderSettings.cs":::
404+
:::code language="csharp" source="~/../blazor-samples/9.0/MauiBlazorWeb/MauiBlazorWeb.Shared/InteractiveRenderSettings.cs":::
409405

410406
A slightly different version of the `InteractiveRenderSettings` class is added to the `.Shared` RCL. In the class added to the `.Shared` RCL, `InteractiveRenderSettings.ConfigureBlazorHybridRenderModes` of the `.Shared.Client` RCL is called. This ensures that the render mode of WebAssembly components rendered on the MAUI client are unassigned (`null`) because they're interactive by default on the native client.
411407

@@ -444,14 +440,15 @@ In `MauiProgram.CreateMauiApp` of `MauiProgram.cs`, call `ConfigureBlazorHybridR
444440
InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();
445441
```
446442

447-
In the `_Imports.razor` file of the `.Shared.Client` RCL, add `@using static InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
443+
In the `_Imports.razor` file of the `.Shared.Client` RCL, replace the `@using` statement for <xref:Microsoft.AspNetCore.Components.Web.RenderMode?displayProperty=fullName> with an `@using` statement for `InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
448444

449-
```razor
450-
@using static InteractiveRenderSettings
445+
```diff
446+
- @using static Microsoft.AspNetCore.Components.Web.RenderMode
447+
+ @using static InteractiveRenderSettings
451448
```
452449

453450
> [!NOTE]
454-
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> via the `@using static Microsoft.AspNetCore.Components.Web.RenderMode` statement in the Blazor Web App's `_Import` file.
451+
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> in the Blazor Web App's `_Import` file.
455452
456453
:::moniker-end
457454

@@ -484,14 +481,15 @@ In `MauiProgram.CreateMauiApp` of `MauiProgram.cs`, call `ConfigureBlazorHybridR
484481
InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();
485482
```
486483

487-
In the RCL's `_Imports.razor` file, add the following global static `@using` directive to make the properties of the class available to components:
484+
In the `_Imports.razor` file of the `.Shared` RCL, replace the `@using` statement for <xref:Microsoft.AspNetCore.Components.Web.RenderMode?displayProperty=fullName> with an `@using` statement for `InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
488485

489-
```razor
490-
@using static InteractiveRenderSettings
486+
```diff
487+
- @using static Microsoft.AspNetCore.Components.Web.RenderMode
488+
+ @using static InteractiveRenderSettings
491489
```
492490

493491
> [!NOTE]
494-
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> via the `@using static Microsoft.AspNetCore.Components.Web.RenderMode` statement in the Blazor Web App's `_Import` file.
492+
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> in the Blazor Web App's `_Import` file.
495493
496494
### Per-page/component Auto interactivity
497495

@@ -525,14 +523,15 @@ In `MauiProgram.CreateMauiApp` of `MauiProgram.cs`, call `ConfigureBlazorHybridR
525523
InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();
526524
```
527525

528-
In the RCL's `_Imports.razor` file, add the following global static `@using` directive to make the properties of the class available to components:
526+
In the `_Imports.razor` file of the `.Shared` RCL, replace the `@using` statement for <xref:Microsoft.AspNetCore.Components.Web.RenderMode?displayProperty=fullName> with an `@using` statement for `InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
529527

530-
```razor
531-
@using static InteractiveRenderSettings
528+
```diff
529+
- @using static Microsoft.AspNetCore.Components.Web.RenderMode
530+
+ @using static InteractiveRenderSettings
532531
```
533532

534533
> [!NOTE]
535-
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> via the `@using static Microsoft.AspNetCore.Components.Web.RenderMode` statement in the Blazor Web App's `_Import` file.
534+
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> in the Blazor Web App's `_Import` file.
536535
537536
### Per-page/component WebAssembly interactivity
538537

@@ -621,36 +620,37 @@ In `MauiProgram.CreateMauiApp` of `MauiProgram.cs`, call `ConfigureBlazorHybridR
621620
InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();
622621
```
623622

624-
In the `_Imports.razor` file of the `.Shared.Client` RCL, add `@using static InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
623+
In the `_Imports.razor` file of the `.Shared.Client` RCL, replace the `@using` statement for <xref:Microsoft.AspNetCore.Components.Web.RenderMode?displayProperty=fullName> with an `@using` statement for `InteractiveRenderSettings` to make the properties of the `InteractiveRenderSettings` class available to components:
625624

626-
```razor
627-
@using static InteractiveRenderSettings
625+
```diff
626+
- @using static Microsoft.AspNetCore.Components.Web.RenderMode
627+
+ @using static InteractiveRenderSettings
628628
```
629629

630630
> [!NOTE]
631-
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> via the `@using static Microsoft.AspNetCore.Components.Web.RenderMode` statement in the Blazor Web App's `_Import` file.
631+
> The assignment of render modes via the RCL's `InteractiveRenderSettings` class properties differs from a typical standalone Blazor Web App. In a Blazor Web App, the render modes are normally provided by <xref:Microsoft.AspNetCore.Components.Web.RenderMode> via in the Blazor Web App's `_Import` file.
632632
633633
:::moniker-end
634634

635635
## Using interfaces to support different device implementations
636636

637637
The following example demonstrates how to use an interface to call into different implementations across the web app and the native (MAUI) app. The following example creates a component that displays the device form factor. Use the MAUI abstraction layer for native apps and provide an implementation for the web app.
638638

639-
In the Razor class library (RCL), an `Interfaces` folder contains an `IFormFactor` interface.
639+
:::moniker range=">= aspnetcore-9.0"
640640

641-
`Interfaces/IFormFactor.cs`:
641+
In the Razor class library (RCL), a `Services` folder contains an `IFormFactor` interface.
642642

643-
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Shared/Interfaces/IFormFactor.cs":::
643+
`Services/IFormFactor.cs`:
644644

645-
:::moniker range=">= aspnetcore-9.0"
645+
:::code language="csharp" source="~/../blazor-samples/9.0/MauiBlazorWeb/MauiBlazorWeb.Shared/Services/IFormFactor.cs":::
646646

647647
The `Home` component (`Components/Pages/Home.razor`) of the RCL displays the form factor and platform.
648648

649649
`Components/Pages/Home.razor`:
650650

651651
```razor
652652
@page "/"
653-
@using MyApp.Shared.Services
653+
@using MauiBlazorWeb.Shared.Services
654654
@inject IFormFactor FormFactor
655655
656656
<PageTitle>Home</PageTitle>
@@ -669,6 +669,12 @@ Welcome to your new app running on <em>@factor</em> using <em>@platform</em>.
669669

670670
:::moniker range="< aspnetcore-9.0"
671671

672+
In the Razor class library (RCL), an `Interfaces` folder contains an `IFormFactor` interface.
673+
674+
`Interfaces/IFormFactor.cs`:
675+
676+
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Shared/Interfaces/IFormFactor.cs":::
677+
672678
The following `DeviceFormFactor` component is present in the RCL's `Components` folder.
673679

674680
`Components/Pages/DeviceFormFactor.razor`:
@@ -693,47 +699,35 @@ The web and native apps contain the implementations for `IFormFactor`.
693699

694700
In the Blazor Web App, a folder named `Services` contains the following `FormFactor.cs` file with the `FormFactor` implementation for web app use.
695701

696-
`Services/FormFactor.cs` (Blazor Web App project):
702+
`Services/FormFactor.cs` (`MauiBlazorWeb.Web` project):
697703

698-
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Web/Services/FormFactor.cs":::
704+
:::moniker range=">= aspnetcore-9.0"
699705

700-
In the MAUI project, a folder named `Services` contains the following `FormFactor.cs` file with the `FormFactor` implementation for native use. The MAUI abstractions layer is used to write code that works on all native device platforms.
706+
:::code language="csharp" source="~/../blazor-samples/9.0/MauiBlazorWeb/MauiBlazorWeb.Web/Services/FormFactor.cs":::
701707

702-
`Services/FormFactor.cs` (MAUI project):
708+
:::moniker-end
703709

704-
:::moniker range=">= aspnetcore-9.0"
710+
:::moniker range="< aspnetcore-9.0"
705711

706-
```csharp
707-
using MauiBlazorWeb.Shared.Interfaces;
712+
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Web/Services/FormFactor.cs":::
708713

709-
namespace MauiBlazorWeb.Services;
714+
:::moniker-end
710715

711-
public class FormFactor : IFormFactor
712-
{
713-
public string GetFormFactor() => DeviceInfo.Idiom.ToString();
716+
In the MAUI project, a folder named `Services` contains the following `FormFactor.cs` file with the `FormFactor` implementation for native use. The MAUI abstractions layer is used to write code that works on all native device platforms.
714717

715-
public string GetPlatform() =>
716-
DeviceInfo.Platform.ToString() + " - " + DeviceInfo.VersionString;
717-
}
718-
```
718+
:::moniker range=">= aspnetcore-9.0"
719719

720-
:::moniker-end
720+
`Services/FormFactor.cs` (`MauiBlazorWeb` project):
721721

722-
:::moniker range="< aspnetcore-9.0"
722+
:::code language="csharp" source="~/../blazor-samples/9.0/MauiBlazorWeb/MauiBlazorWeb/Services/FormFactor.cs":::
723723

724-
```csharp
725-
using MauiBlazorWeb.Shared.Interfaces;
724+
:::moniker-end
726725

727-
namespace MauiBlazorWeb.Maui.Services;
726+
:::moniker range="< aspnetcore-9.0"
728727

729-
public class FormFactor : IFormFactor
730-
{
731-
public string GetFormFactor() => DeviceInfo.Idiom.ToString();
728+
`Services/FormFactor.cs` (`MauiBlazorWeb.Maui` project):
732729

733-
public string GetPlatform() =>
734-
DeviceInfo.Platform.ToString() + " - " + DeviceInfo.VersionString;
735-
}
736-
```
730+
:::code language="csharp" source="~/../blazor-samples/8.0/MauiBlazorWeb/MauiBlazorWeb.Maui/Services/FormFactor.cs":::
737731

738732
:::moniker-end
739733

0 commit comments

Comments
 (0)