Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin表单处理Webview导航问题中的超链接_C#_Xamarin_Xamarin.ios_Xamarin.android_Xamarin.forms - Fatal编程技术网

C# Xamarin表单处理Webview导航问题中的超链接

C# Xamarin表单处理Webview导航问题中的超链接,c#,xamarin,xamarin.ios,xamarin.android,xamarin.forms,C#,Xamarin,Xamarin.ios,Xamarin.android,Xamarin.forms,我正在开发一个应用程序,其中包含一些来自CMS的内容页面 这需要显示在WebView中,因为它们有带有一些超链接的HTML 我在控制超链接,它在安卓系统上的工作与预期一样 我是通过Xamarin.Forms.WebView事件的导航来实现这一点的 我可以使用e.Url将用户发送到内部页面 示例: public class HybridWebView : WebView { public String Code { get; set; } public HybridWebView

我正在开发一个应用程序,其中包含一些来自CMS的内容页面 这需要显示在
WebView
中,因为它们有带有一些超链接的HTML

我在控制超链接,它在安卓系统上的工作与预期一样 我是通过Xamarin.Forms.WebView事件的
导航来实现这一点的

我可以使用
e.Url
将用户发送到内部页面

示例:

public class HybridWebView : WebView
{
    public String Code { get; set; }

    public HybridWebView()
    {
        Navigating += HybridWebView_Navigating;
    }


    private void HybridWebView_Navigating(object sender, WebNavigatingEventArgs e)
    {
       if(e.Url.EndsWith(".aspx")
       {
          var CMS = new CMSView(e.Code, null);
          Navigation.PushAsync(CMS);
          e.Cancel = true;
       }
    }
}
请注意,这很有效

问题是:

当我在iOS上尝试时,我得到了一个空白/白色
WebView
。 我尝试在iOS上创建CustomRenderer,但由于我将WebView子类化以自定义它,因此无法访问属性
code

如何访问ios自定义渲染器中的属性?我知道
e.NewElement
,但是
应该加载
需要UIWebView,而不是我的自定义HybridWebView

有人有什么建议或想法吗?我如何做到这一点

提前感谢

在您的客户渲染器(我假设是WebViewRenderer的子类)中,您可以将e.newElement强制转换为您的HybridWebView类型,例如:

HybridWebView wv = e.NewElement as HybridWebView;
然后,您应该能够使用
wv.code
访问您的
code
属性

另外,
WebViewRenderer
NativeView
属性将为您提供要使用的iOS native
UIView
。您应该能够将其强制转换为UIWebView而不会出现问题。例如:

UIWebView nativeUIWebView = NativeView as UIWebView;

我希望这有帮助

如果要使用
shouldstartoad
,则需要为正在使用的本机
UIWebView
实现自定义
UIWebViewDelegate
。这可能会变得棘手,因为如果用自定义委托替换现有委托,将丢失Xamarin.Forms
WebView
提供的一些内置功能。以下是我用来充分利用两者的策略:

[assembly: ExportRenderer (typeof (WebView), typeof (CustomWebViewRenderer))]
public class CustomWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged (VisualElementChangedEventArgs e)
    {
        base.OnElementChanged (e);

        if (e.OldElement == null) {

            // Need to capture the existing one so we don't loose functionality 
            // that Xamarin.Forms already has
            var existingDelegate = Delegate;

            // Make sure to pass the existing one to the new one
            Delegate = new MyCustomWebViewDelegate(existingDelegate);
        }
    }
}

public class MyCustomWebViewDelegate : UIWebViewDelegate
{
    readonly UIWebViewDelegate existingDelegate;

    public MyCustomWebViewDelegate(UIWebViewDelegate existing)
    {
        existingDelegate = existing;
    }

    public override bool ShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
    {
        // You can customize this instead of returning the existingDelegate method
        return existingDelegate.ShouldStartLoad(webView, request, navigationType);
    }

    public override void LoadFailed (UIWebView webView, NSError error)
    {
        existingDelegate.LoadFailed(webView, error);
    }


    public override void LoadingFinished (UIWebView webView)
    {
        existingDelegate.LoadingFinished(webView);
    }


    public override void LoadStarted (UIWebView webView)
    {
        existingDelegate.LoadStarted(webView);
    }

}
注意*在iOS上,WebViewRenderer直接从UIWebView继承,因此您不需要使用控件属性。您是UIWebView的一个直接子类,可以访问所需的属性。见资料来源


您可以将自定义登录添加到
MyCustomWebViewDelegate.ShouldStartLoad
方法。

Thnx jgoldberger我不需要强制转换e.NewElement,因为它知道它来自hybridview的类型,但是我如何使用ShouldStartLoad呢?它希望UIWebView作为参数,我需要在那里使用hybridview。除非我遗漏了什么,我提到的NativeView应该是基本渲染器为HybridWebView创建的。所以这应该是与ShouldStartLoad一起使用的UIWebView,除非我有误解?