如何修改 Xamarin.Forms 中的 WebView 以在设备上的浏览器中打开链接? [英] How can I modify a WebView in Xamarin.Forms to open links in a browser on the device?

查看:37
本文介绍了如何修改 Xamarin.Forms 中的 WebView 以在设备上的浏览器中打开链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认链接 () Xamarin.Forms WebViews 在 WebView 内打开.尤其是在没有原生后退按钮的 iOS 中,当从互联网打开 URL 时,行为很不方便.

By default links (<href..>) Xamarin.Forms WebViews open inside the WebView. Especially in iOS where there's no native backbutton that behavior is unconveniend when opening an Url from the internet.

如何让 Xamarin.Forms 改为让设备上的浏览器打开链接,使其至少适用于 Android、iOS 和 UWP?

How do I get Xamarin.Forms to let a browser on the device open the links instead, so that it works in at least Android, iOS and UWP?

推荐答案

没有内置属性或任何东西可以让您做到这一点.

There is no built-in property or anything that lets you do this.

然而,WebView 确实有一个 Navigating 事件处理程序.您应该能够挂钩,将用户重定向到您想要的任何内容,然后取消原始事件.像这样:

However, the WebView does have a Navigating event handler. You should be able to hook into that, redirect the user to whatever you want and then cancel the original event. Something like this:

public void WebView_Navigating(object sender, WebNavigatingEventArgs args)
{
    if (args.Url.StartsWith("file://"))
    {
        return;
    }

    Device.OpenUri(new Uri(args.Url));

    args.Cancel = true;
}

从代码中连接起来:

var webView = new WebView();
webView.Navigating += WebView_Navigating;

来自 XAML:

<WebView Navigating="WebView_Navigating" />

更多信息:

https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.webview.navigating?view=xamarin-forms

这篇关于如何修改 Xamarin.Forms 中的 WebView 以在设备上的浏览器中打开链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆