8

I am new to .Net MAUI but fairly experienced with Blazor. I am starting this new project and I'm intending to get the benefits of MAUI.

My issue is, I'd like to use some native controls from MAUI, an example is the Bottom Sheet, but I don't know how to use these controls in Blazor.

Is there a way to use MAUI's controls in the .Net Blazor MAUI app?

4
  • 3
    I think the way is to embed Blazor into a MAUI app. I don't know anything about the other direction. Commented Oct 10, 2022 at 14:00
  • 1
    Better way is full use of blazor in Blazor MAUI apps as UI handler. Then you should use blazor components and implement what you need Commented Oct 10, 2022 at 14:10
  • @ARTAV, that's interesting. But would this process cause me to write XAML? Commented Oct 10, 2022 at 14:13
  • 1
    Better still, do you mind writing an answer to explain the process of doing it? I'll be grateful Commented Oct 10, 2022 at 14:20

5 Answers 5

8

You can´t use MAUI components (xaml) inside the blazor pages. BUT, if needed, you can have a XAML view mixing the blazorwebview with other MAUI elements. I do that to read QR codes: I have in the Main view a Fragment that have a MAUI QR reader, that I make visible only when a button in the Blazor page is pressed, I even place that MAUI Fragment overlaying the Blazor view.

Something else you should know is that you can communicate from MAUI to Blazor, and vice versa, by using a common object injected with the DI facilities. This object would have Methods and Events to allow this communication.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @gonzalorf! Can you send me code of your Main view with Fragment that have a MAUI QR reader. It'd really help me.
Sure @AnnaL I made a demo project, you can get it here: github.com/gonzalorf/Maui-Blazor-QRReader
Can I use Maui Map Component inside a blazor page? I can't seem to make it work.
@Marin, as comments say, you don’t/can’t put native controls inside Blazor page/view. Instead, you make a Maui page, which contains both a Blazor view and the desired native control. Optionally can add other Maui elements. For example, might use Maui AppShell or a Maui navigation bar at top or side, and switch the bulk of the page between a Blazor component and the native component. Bottom line: don’t try to do everything inside Blazor. Learn some Maui also.
4

You can use the MAUI components in xaml pages. Then you can embed BlazorWebView controls as needed.

here is the MainPage.xaml with the embedded BlazorWebView:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:b="clr-namespace:Microsoft.AspNetCore.Components.WebView.Maui;assembly=Microsoft.AspNetCore.Components.WebView.Maui"
             xmlns:local="clr-namespace:MauiBlazorApp"
             x:Class="MauiBlazorApp.MainPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}">

    <b:BlazorWebView HostPage="wwwroot/index.html">
        <b:BlazorWebView.RootComponents>
            <b:RootComponent Selector="app" ComponentType="{x:Type local:Main}" />
        </b:BlazorWebView.RootComponents>
    </b:BlazorWebView>

</ContentPage>

Comments

4

TL;DR You can get access to some native dialog controls from the Application class provided out of the box.

public async Task<bool> DisplayConfirm(string title, string message, string accept, string cancel)
{
    return await Application.Current.MainPage.DisplayAlert(title, message, accept, cancel);
}

So I was referred to this YouTube video that does exactly what I wanted to do. Have a look: https://www.youtube.com/watch?v=gTLEIe0SWpI

Comments

1

.NET MAUI apps are native on each mobile/desktop platform - they welcome Blazor/web content through modern WebViews. .NET MAUI native components can be mix & matched with Blazor content - essentially a BlazorWebView hosted inside a XAML page renders web content. Nothing stopping developers from rendering native UI through XAML mixed in with Blazor UI - there are .NET MAUI UI components that can help. One can also drive more of the app UI through Blazor and share code between web/native apps.

1 Comment

IMHO, the phrase "rendering native UI through XAML mixed in with Blazor UI" can easily be misunderstood. Other answers more clearly describe the situation: Maui controls (or native UI wrapped in Maui controls) are OUTSIDE the Blazor UI; all Blazor UI is inside a BlazorWebView, which is within the app's hierarchy of Maui controls. The "mixing" is limited to one direction: Blazor inside Maui UI, not Maui UI inside Blazor.
1

You can use the Native .NET MAUI controls inside XAML. Next to those, you can have a BlazorWebView control that hosts the Blazor part of your page.

You can use one of the approaches listed on https://stackoverflow.com/a/77440099/1114918 to communicate across the Blazor / XAML boundary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.