mvvmbindingmvvmcross

Dynamic Binding UIWebView in MVVMCross


I'm trying to make a change to sample project Cirrious.Conference. In particular in the Touch View at SessionView class and at this class

https://github.com/slodge/MvvmCross-Tutorials/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.Core/ViewModels/SessionLists/BaseSessionListViewModel.cs

on method

protected void NavigateToSession(Session session)
{
 ShowViewModel<SessionViewModel>(new { key = session.Key });
}

I'd like to openl a UIWebView (in app) binding LoadRequest with a property of class Session (suppose to have a Property URL...). I have created a UIWebView object in the SessionView but it's not possible to create a Swisse Binding...Maybe it's possible with a customBinding...

How could i'll do it?


Solution

  • Since UIWebView doesn't expose a property for the LoadRequest, then you can't bind directly to it.

    If you want to use binding for LoadRequest, then 3 options available to you are:

    1. Inherit MyWebView from UIWebView, add a C# property that drives LoadRequest and then use that class in your UI and that property in your Swiss binding - e.g.:

           [Register("MyWebView")]
           public class MyWebView : UIWebView
           {
               public MyWebView()
               {
               }
      
               public MyWebView(IntPtr handle) : base(handle)
               {
               }
      
               private string _myUrl;
               public string MyUrl
               {
                   get { return _myUrl; }
                   set
                   {
                      if (_myUrl == value) return;
                      _myUrl = value;
                      LoadRequest(value); // or similar (I've not checked the syntax!)
                   }
               }
           }
      
    2. Implement a custom target Swiss binding and add it to your Setup.cs. The process for this is described in this Custom Bindings presentation - which also includes links to some examples (one of them is in the Conference app)

    3. If this property will never change, then don't use binding and instead just call LoadRequest in your MvxViewController ViewDidLoad - e.g.

            public void ViewDidLoad()
            {
                base.ViewDidLoad();
      
                var myViewModel = (MyViewModel)ViewModel;
                var url = myViewModel.Url;
                TheWebView.LoadRequest(url);
            }