-
Notifications
You must be signed in to change notification settings - Fork 25.2k
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
Blazor Hybrid: Reuse Razor components topic #25675
Comments
Content is here How to author Blazor components for the web and webviewBlazor runs in multiple environments or hosts; Webassembly, Server and Webview. Each host has unique capabilities in the framework and in the platform that components can leverage, but that can come at the cost of not working well in other hosts. For example, Blazor webassembly supports synchronous interop which is not supported in other hosts like server or webview as the communication channel between JavaScript and .NET is strictly asynchronous. Another example can be how a component in Blazor Server can access services that are only available on the server, like entity framework DbContext. Much in the same way, a component in Blazor Webview can access funcionality on the device it is running like the geolocation or other native functionality offered by the platform that Blazor Server and Webassembly have to rely on the web interfaces to get access to. Common patternsIn order to author components that can seamlessly work across different hosts we need to follow some design principles to make it easier for us to adapt those components to each specific host.
Code organization in projectsAs much as possible the code and static content should be inside a Razor Class Library. Each target platform (Webassembly, Server, Webview) references the Razor Class Library and registers on the DI container implementations specific for that platform that a component might require. Each target platform assembly should contain only the code that is specific to that platform and the code that helps boostrap the application. flowchart LR
Webassembly & Server & Webview -- ProjectReference --> RCL
Abstract platform speicific functionalityflowchart RL
subgraph RCL
A["MapComponent"] -.Injects.-> B
B[ILocationService]
end
subgraph Web ["App.Web (webassembly & Server)"]
C["WebLocationService : ILocationService"] -- Implements --> B
end
subgraph Native ["App.Desktop (Maui, WPF, WindowsForms)"]
D["DesktopLocationService : ILocationService"] -- Implements --> B
end
Platform specific code on Blazor Maui applicationsA very common pattern in Maui involves having different implementations for different platforms following several patterns like defining partial classes with platform specific implementations. For example: flowchart TD
subgraph CameraService.cs
A[partial class CameraService]
end
subgraph CameraService.Android.cs
B[partial class CameraService]
end
subgraph CameraService.iOS.cs
C[partial class CameraService]
end
subgraph CameraService.Windows.cs
D[partial class CameraService]
end
In these cases, where you want to pack the functionality in a class library that can be consumed by other applications using Maui Blazor, we recommend you follow a similar approach to the one described above and create an abstraction for your component and put it in a Razor Class Library. Then on a Maui Class Library, reference the Razor Class Library and create your platform specific implementations. Finally, within the consuming application, reference the Maui Class Library. flowchart
subgraph RCL ["Razor Class Library"]
A["ICameraService"]
F["InputPhoto"] -- Injects --> A
end
subgraph MCL ["Maui Class Library"]
B["partial CameraService.Android.cs"] -- Implements --> A
D["partial CameraService.Windows.cs"] -- Implements --> A
E["partial CameraService.IOS.cs"] -- Implements --> A
end
subgraph MBA ["Maui Blazor Application"]
Main -- Uses --> F
end
MBA --> MCL
MCL --> RCL
|
We should point out to the Podcasts sample |
@javiercn ... I need the link to the podcast that you mentioned ☝️ ... or to just the sample if that's what you want. |
@guardrex it's not a podcast, it's a sample app that does web, maui and maui-blazor https://github.com/microsoft/dotnet-podcasts |
When content is provided in an issue comment here, please remove the PU member assignment and assign the issue to @guardrex 🦖. PU member will be pinged on the PR when it goes up.
NOTE TO SELF:
The text was updated successfully, but these errors were encountered: