Xamarin.Forms.WebView.Navigating事件在iOS上引发以进行内部导航 [英] Xamarin.Forms.WebView.Navigating event raised on iOS for internal navigation

查看:176
本文介绍了Xamarin.Forms.WebView.Navigating事件在iOS上引发以进行内部导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您要阻止用户从Xamarin.Forms.WebView导航到外部页面.

Let's say you want to prevent the user from navigating away from your Xamarin.Forms.WebView to an external page.

public App ()
{
    var webView = new WebView
    {
        Source = new HtmlWebViewSource
        {
            Html = "<h1>Hello world</h1><a href='http://example.com'>Can't escape!</a><iframe width='420' height='315' src='https://www.youtube.com/embed/oHg5SJYRHA0' frameborder='0' allowfullscreen></iframe>"
        }
    };
    webView.Navigating += WebView_Navigating;

    MainPage = new ContentPage {
        Content = webView
    };
}

private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
{
    // we don't want to navigate away from our page
    // open it in a new page instead etc.
    e.Cancel = true;
}

在Windows和Android上运行正常.但是在iOS上,它根本无法加载!

This works fine on Windows and Android. But on iOS, it doesn't load at all!

在iOS上,即使从HtmlWebViewSource加载具有类似file:///Users/[user]/Library/Developer/CoreSimulator/Devices/[deviceID]/data/Containers/Bundle/Application/[appID]/[appName].app/

On iOS, the Navigating event gets raised even when loading the source from a HtmlWebViewSource, with a URL that looks something like file:///Users/[user]/Library/Developer/CoreSimulator/Devices/[deviceID]/data/Containers/Bundle/Application/[appID]/[appName].app/

好的,所以您可以通过以下方法解决此问题:

Alright, so you can get around that with something like this:

private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
{
    if (e.Url.StartsWith("file:") == false)
        e.Cancel = true;
}

该页面最终在iOS上加载.耶.可是等等!嵌入式YouTube视频无法加载!这是因为 Navigating事件是针对嵌入式资源(例如iframe)甚至外部脚本(例如Twitter的<script charset="utf-8" type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>)的内部导航引发的,但仅限于iOS!

The page finally loads on iOS. Yay. But wait! The embedded YouTube video doesn't load! That's because the Navigating event gets raised for the internal navigation of embedded resources like iframes and even external scripts (like Twitter's <script charset="utf-8" type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>), but only on iOS!

我找不到确定导航事件是由内部导航引发还是由于用户单击了链接的方法.

I couldn't find a way to determine if the Navigating event was raised from internal navigation or because the user clicked a link.

如何解决这个问题?

推荐答案

我不确定是否可以开箱即用Xamarin Forms进行检测,但是可以使用自定义渲染器轻松确定导航类型.在您的自定义iOS渲染器中,分配一个WebViewDelegate,然后在该Delegate类中,覆盖ShouldStartLoad(),如下所示:

I am not sure if it is possible to detect in Xamarin Forms out of the box but the navigation type is easily determined using a custom renderer. In your custom iOS renderer, assign a WebViewDelegate and within that Delegate class, override ShouldStartLoad() like so:

public class CustomWebViewRenderer : WebViewRenderer {

    #region Properties

    public CustomWebView CustomWebViewItem { get { return Element as CustomWebView; } }

    #endregion

    protected override void OnElementChanged(VisualElementChangedEventArgs e) {
        base.OnElementChanged(e);

        if(e.OldElement == null) {
            Delegate = new CustomWebViewDelegate(); //Assigning the delegate
        }
    }
}
internal class CustomWebViewDelegate : UIWebViewDelegate {

    public override bool ShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType) {

        if(navigationType == UIWebViewNavigationType.LinkClicked) {
            //To prevent navigation when a link is click, return false
            return false;
        }

        return true;
    }
}

您还可以显示bool属性,甚至可以枚举备份到Xamarin Forms WebView,这将说明Navigating事件是来自单击的链接还是来自其他链接,尽管需要自定义渲染器为此.

You could also surface a bool property or even an enum back up to your Xamarin Forms WebView which would say whether the Navigating event was from a link being clicked or from something else, though a custom renderer would be needed for that as well.

这篇关于Xamarin.Forms.WebView.Navigating事件在iOS上引发以进行内部导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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