Maui+大麻杂种:手柄后按钮按在Android上
我有一个使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了与您相同的问题,Microsoft尚未支持毛伊岛大麻的钥匙导航,但是我找到了一个临时解决方案:
首先,您必须创建一个JavaScript文件,并将其放入wwwroot文件夹中,并包括它在index.html文件中。在JS文件中放置此代码:
然后您必须创建AC#类并使其成为服务,例如,我创建了一个名为GOBACK.CS的类。要将其设置为服务,只需将此代码放入mauiprogram.cs:
在GOBACK类中,您必须创建一个静态变量和方法,因为否则您将无法使用IJSruntime Interop。我的回收文件如下:
方法GOBACKINTIME将通过JSInteraperability调用JS方法回击。现在,您必须在加载此服务的第一个大火页面中注入。
这样,CS文件中的jsruntime将被初始化而不是null。
现在,您要做的就是覆盖mainpage.xaml.cs中的onbackButtonPressEd方法:
这样,当按下返回按钮时,它不会退出应用程序(因为我们返回了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:
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:
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:
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.
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:
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.