Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

xamarin - ShouldStartLoad of custom UIWebViewDelegate not being called when UIWebView request starts loading

I have defined a UIWebViewDelegate class and instantiated it in the Delegate property of my UIWebView, but the ShouldStartLoad method is not being called when the webview loads a request. What am I doing wrong?

Here is the code defining my UIWebViewDelegate and my UIWebView:

public class MyWebViewDelegate: UIWebViewDelegate
{
    private UIWebView _view;
    public MyWebViewDelegate (UIWebView view)
    {
         _view = view;
    }   
    public override bool ShouldStartLoad (UIWebView webView, 
                                          MonoTouch.Foundation.NSUrlRequest request,
                                          UIWebViewNavigationType navigationType)
    {
        System.Console.WriteLine("Starting load");
        return true;
    }
}

public class MyWebView : UIWebView
{
   private static MyWebView _instance = new MyWebView ();
   private MyWebView () : base()
   {
       this.Delegate = new MyWebViewDelegate(this);
   }
   public static MyWebView Instance {
       get { return _instance; }
   }
   public void Load ()
   {
       this.LoadRequest (new NSUrlRequest(NSUrl.FromString("http://myurl"),
                           NSUrlRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
                           5)
                        );
   }
}

In my appdelegate I am then doing the following in FinishedLaunching method:

// Create form view controller
viewControllerForm = new UIViewController();
viewControllerForm.View = MyWebView.Instance;
viewControllerForm.Title = Localization.Translate("Forms");

// Load webview
MyWebView.Instance.Load();

Note that I have used copy/paste to give this code sample, so there could be some minor copy/paste errors here, but my code is compiling and the webview loads the request; it is just that the ShouldStartLoad that is not being called.

Does anyone have any idea what I am doing wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved this by making use of the ShouldStartLoad property of the webview as follows:

myView.ShouldStartLoad = (webView, request, navType) => {
     // Determine here what to do
}

I got this answer from miguel.de.icaza's answer to the question posted here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...