Mvvmcross: Binding : Binding Set Creation

Created on 17 Jan 2020  ·  9Comments  ·  Source: MvvmCross/MvvmCross

🏗 Enhancement Proposal

Simplify MvxFluentBindingDescriptionSet creation.

Pitch

The current binding set creation:

class SomeView : MvxViewController<SomeViewModel>
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var set = this.CreateBindingSet<SomeView, SomeViewModel>();
        set.Apply();
    }
}

Could be simplified for Views that inherited from base View classes with generic View Model argument:

class SomeView : MvxViewController<SomeViewModel>
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var set = CreateBindingSet();
        set.Apply();
    }
}

With an additional method in base View class, like:

class MvxViewController<TViewModel> : MvxViewController, IMvxIosView<TViewModel> where TViewModel : class, IMvxViewModel
{
    protected MvxFluentBindingDescriptionSet<MvxViewController<TViewModel>, TViewModel> CreateBindingSet()
    {
        return new MvxFluentBindingDescriptionSet<MvxViewController<TViewModel>, TViewModel>(this);
    }
}

Platforms affected (mark all that apply)

  • [x] :iphone: iOS
  • [x] :robot: Android
  • [x] :checkered_flag: WPF
  • [x] :earth_americas: UWP
  • [x] :apple: MacOS
  • [x] :tv: tvOS
  • [x] :monkey: Xamarin.Forms
enhancement up-for-grabs

All 9 comments

You are welcome to open a PR adding this.

Should CreateBindingSet() extend IMvxIosView rather than MvxViewController?

protected MvxFluentBindingDescriptionSet<IMvxIosView<TViewModel>, TViewModel> CreateBindingSet()
{
    return new MvxFluentBindingDescriptionSet<IMvxIosView<TViewModel>, TViewModel>(this);
}

IMvxIosView also inherits from IMvxBindingContextOwner, so i think the above should work in the same way

https://github.com/MvvmCross/MvvmCross/blob/bdaa09299714d94cf3f2c548a465d994c20d52f0/MvvmCross/Platforms/Ios/Views/IMvxIosView.cs#L11-L14

Why?

Not all the MvxControllers inherit off MvxViewController. For example MvxBaseSplitViewController

Another good reason to extend IMvxView<TViewModel>: this forces us to add this new method to each implementing view, so will not forget any of them. I will prepare a PR.

@Prin53 do you mean IMvxView<TViewModel> or IMvxIosView<TViewModel>

I don't think there is any need to extend IMvxView?

@FinHorsley the first one, as there are people who using these fluent bindings on different platforms (at least on Android). If “bindings” is not a concern of this base interface we can extend base platform interfaces, like IMvxIosView<T>, IMvxAndroidView<T> and so on. What do you think?

Use IMvxView<TViewModel>. Fluent bindings are not only for iOS.

We can extend IMvxPlatformView<T> only, as we need IMvxBindingContextOwner implementation.

Why was this not implemented in IMvxView<TViewModel>?
Just asking out of curiosity.

@MarLoe see my comment above: we need IMvxBindingContextOwner to create a binding set.

Was this page helpful?
0 / 5 - 0 ratings