2016-07-22 30 views
1

林發展具有不同於我們的CMS 一些內容的頁面需要被顯示成WebView一個應用程序,因爲他們有HTML,其某些超鏈接Xamarin表單處理的超鏈接在網頁視圖導航問題

林控制的超鏈接至極正如預期在Android 即時通訊Xamarin.Forms.WebView事件

即時通過Navigating這樣做可以使用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,但ShouldStartLoad預計UIWebView,而不是我的自定義HybridWebView。

有沒有人有任何提示或想法,我可以達到這個。

在此先感謝

回答

3

在你的客戶渲染(WebViewRenderer的一個子類我假設),你可以投e.newElement您HybridWebView類型,例如:

HybridWebView wv = e.NewElement as HybridWebView; 

那麼你應該能夠訪問您的Code財產wv.Code

此外WebViewRendererNativeView屬性會給你的iOS本地UIView工作。你應該能夠將這個投影到一個UIWebView而沒有問題。例如:

UIWebView nativeUIWebView = NativeView as UIWebView; 

我希望這有助於!

+0

Thnx jgoldberger我不需要投e.NewElement,因爲它知道它的類型的hybridview,但我怎麼能使用ShouldStartLoad它期望UIWebView作爲參數,我需要在那裏使用Hybridview。 –

+0

除非我遺漏了一些東西,否則我提到的NativeView應該是基礎渲染器爲您的HybridWebView創建的。所以這應該是UIWebView與ShouldStartLoad一起使用,除非我誤解? – jgoldberger

1

如果您要使用ShouldStartLoad,則需要爲使用的本地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的直接子類,可以訪問您期望的屬性。見源here

您可以將自定義登錄添加到MyCustomWebViewDelegate.ShouldStartLoad方法。