我有一个MAUI应用程序,本质上是一个托管WordPress网站的WebView
。我的问题是,每当我试图加载外部链接时,它都不会加载“href”重定向...它在Windows和iOS设备上运行良好,但在Android中不是。
EDIT:href是一个“_blank”目标。
清单中是否有我需要的特定权限?
下面是该页面的代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MYAPPNAME.EnglishPage"
NavigationPage.HasNavigationBar="False" NavigationPage.HasBackButton="False"
Shell.NavBarIsVisible="False">
<Grid BackgroundColor="DarkSlateGrey">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0"
Spacing="5" Orientation="Horizontal" Padding="8">
<Button Text="Go Back" TextColor="DarkSlateGray" BackgroundColor="White"/>
<Button Text="Change Language" x:Name="ChangeLangBtn" TextColor="DarkSlateGray" BackgroundColor="White"/>
</StackLayout>
<WebView Grid.Row="1" Grid.Column="0"
Source="https://example.com"
x:Name="WebView"/>
</Grid>
</ContentPage>
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="2" android:versionName="2">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:label="MYAPPNAME"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
</manifest>
2条答案
按热度按时间gudnpqoy1#
我已经做了一个例子来测试在android的webview,它可以成功地打开在网站中的href。
所以你可以检查你要打开的href是否是http,对于27以上的android api,你需要在AndroidManifest.xml中声明
android:usesCleartextTraffic
为true,比如:您也可以尝试在您的webview中加载另一个网站进行测试。
编辑
引用这个native android case和这个custom webview handler,你就可以试着用这个句柄重写
ShouldOverrideUrlLoading
方法来做到这一点。5rgfhyps2#
So to handle redirects to
_blank
targets in a WebView by keeping them in the same window/view, I had to create a custom renderer, but don't worry, you can just copy-paste the code and it will work perfectly :) .Controls
insidePlatforms/Android
, then create a class file, I called itMltWebViewClientRenderer.cs
. Paste this inside the file:The above code basically allows access to the
Settings
of theWebView
.MauiProgram.cs
, you need to add some services to the builder, as such:The above only invokes our custom renderer if we are on Android.
Note: This solution only works for Android API versions 24 and above.