Maui+大麻杂种:手柄后按钮按在Android上

发布于 2025-02-02 21:46:16 字数 1019 浏览 5 评论 0原文

我有一个使用MAUI Hybrid生产Android应用程序(APK)的项目。 BlazorWebView在MAUI应用程序中运行,因此网站(UN Blazor Wasm)和Maui App(Maui + Blazor WebView)都使用了页面代码。

MAUI应用程序(来自mainpage.xaml的代码)中包含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:MyMauiBlazor"
             x:Class="MyMauiBlazor.MainPage">

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

</ContentPage>

on Android设备,是否可以 处理后按钮按下

当前,该应用程序关闭,但我想处理此事件(例如:导航到上一页)。 有办法这样做吗?

I have a project using MAUI Hybrid to produce an Android Application (apk).
A blazorWebView is running in MAUI app, so that the code of pages is used by both Web Site (un Blazor Wasm) and MAUI app (MAUI + Blazor WebView).

A blazorWebView is included in MAUI App (code from MainPage.xaml) :

<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:MyMauiBlazor"
             x:Class="MyMauiBlazor.MainPage">

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

</ContentPage>

On Android devices, is it possible to handle the Back Button pressed ?

Currently, the App closes, but I want to handle this event (ex : navigate to previous page).
Is there a way to do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2025-02-09 21:46:16

我遇到了与您相同的问题,Microsoft尚未支持毛伊岛大麻的钥匙导航,但是我找到了一个临时解决方案:

首先,您必须创建一个JavaScript文件,并将其放入wwwroot文件夹中,并包括它在index.html文件中。在JS文件中放置此代码:

window.goBack = () => {
   history.go(-1);
}

然后您必须创建AC#类并使其成为服务,例如,我创建了一个名为GOBACK.CS的类。要将其设置为服务,只需将此代码放入mauiprogram.cs:

builder.Services.AddTransient<GoBack>();

在GOBACK类中,您必须创建一个静态变量和方法,因为否则您将无法使用IJSruntime Interop。我的回收文件如下:

public class GoBack 
{
    private static IJSRuntime JSRuntime { get; set; }

    public GoBack(IJSRuntime jSRuntime)
    {
        GoBack.JSRuntime = jSRuntime;
    }
   
    public static async Task GoBackInTime()
    {
        //Microsoft.Maui.Platform;
        if (GoBack.JSRuntime != null)
        {
            await GoBack.JSRuntime.InvokeVoidAsync("goBack");
        }
    }

    
}

方法GOBACKINTIME将通过JSInteraperability调用JS方法回击。现在,您必须在加载此服务的第一个大火页面中注入。

@inject GoBack goBack

这样,CS文件中的jsruntime将被初始化而不是null。

现在,您要做的就是覆盖mainpage.xaml.cs中的onbackButtonPressEd方法:

protected override bool OnBackButtonPressed()
{
    GoBack.GoBackInTime();
    return true;
}

这样,当按下返回按钮时,它不会退出应用程序(因为我们返回了TRUE),同时js函数被称为,以便开拓者页面将导航到上一页。

I had the same problem as you, officially microsoft doesn't support back key navigation for MAUI blazor yet, but I found a temporary solution for that:

First you have to create a javascript file, and put it in the wwwroot folder, and include it in the index.html file. Inside the js file put this code:

window.goBack = () => {
   history.go(-1);
}

Then you have to create a c# class and make it a service, so for example I created a class called GoBack.cs. To set it as a service just put this code in MauiProgram.cs:

builder.Services.AddTransient<GoBack>();

In the GoBack class you have to create a static variable and method, because otherwise you will not be able to use the IJSRuntime interop. My GoBack file is the following:

public class GoBack 
{
    private static IJSRuntime JSRuntime { get; set; }

    public GoBack(IJSRuntime jSRuntime)
    {
        GoBack.JSRuntime = jSRuntime;
    }
   
    public static async Task GoBackInTime()
    {
        //Microsoft.Maui.Platform;
        if (GoBack.JSRuntime != null)
        {
            await GoBack.JSRuntime.InvokeVoidAsync("goBack");
        }
    }

    
}

The method GoBackInTime is going to call the js method goBack through the jsInteroperability. Now you have to inject in the first blazor page that you load this service.

@inject GoBack goBack

This way the jsRuntime in the cs file will be initialized and not null.

Now all you have to do is override the OnBackButtonPressed method in MainPage.xaml.cs:

protected override bool OnBackButtonPressed()
{
    GoBack.GoBackInTime();
    return true;
}

This way, when the back button is pressed, it won't exit the application (because we are returning true) and at the same time the js function is called so the blazor page will navigate to the previous page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文