user17521524
user17521524

Reputation: 1

Can Blazor Server App + MAUI access device?

Is Blazor Server App runnng in a MAUI WebView going to be able to interact with the native device using something like JS Interop?

Upvotes: 0

Views: 1034

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34063

Absolutely and you don't need JS interop. It just runs on the platform so you can leverage everything that's there.

Depending on your architecture and how you structure your code there is multiple ways to do things. If it's something that is already in Essentials for instance, you can just add .NET MAUI Essentials and use those APIs. I have a video on that here and the code is here.

If you really need to do something else you can leverage the dependency injection. For instance, in the repo I linked (which is mostly a File > New Project), you can see in MauiProgram.cs how the WeatherForecastService is registered: builder.Services.AddSingleton<WeatherForecastService>();

Now in your Blazor code you can just let that be injected through @inject WeatherForecastService ForecastService and use it as:

@code {
    private WeatherForecast[] forecasts;
    private Location location;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }
}

You can make your own service that way to access platform sensors or APIs, inject it like this and use it from Blazor.

Upvotes: 2

Related Questions