Justin Neff
Justin Neff

Reputation: 182

Xamarin MvvmCross Fluent Binding on UWP

I am making a Windows UWP app and I'm trying to do Fluent Binding on the View, MvxWindowsPage. When I declare the MvxFluentBindingDescriptionSet

var set = this.CreateBindingSet<FirstView, FirstViewModel>();

it gives a build time error of:

The type 'FirstView' cannot be used as type parameter 'TTarget' in the generic type or method There is no implicit reference conversion from 'FirstView' to 'MvvmCross.Binding.BindingContext.IMvxBindingContextOwner'.

My WindowsPage declaration is as follows:

using MvvmCross.Uwp.Views;
using MyApp.Core.ViewModels;
using MvvmCross.Binding.BindingContext;
public sealed partial class FirstView : MvxWindowsPage<FirstViewModel>
{
    public FirstView()
    {
        this.InitializeComponent();
        //This line below gives me the build time error
        var set = this.CreateBindingSet<FirstView, FirstViewModel>();

        set.Bind(myButton)
            .To(viewmodel => viewmodel.DoStuffSelected);
    }
}

How do I do Fluent Binding in a UWP App? Or is the support for a UWP app only for Forms bindings?

Upvotes: 2

Views: 464

Answers (1)

Jason Willett
Jason Willett

Reputation: 414

I was able to work around this by looking at how they implemented this fix in WPF. Not sure if this is 100% correct, but it worked for me implementing MvxInteraction in a MvxWindowsPage view (using MvvmCross v6.2.3):

using ExampleApp.Uwp.Interactions;
using ExampleApp.Uwp.ViewModels;
using MvvmCross.Base;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Platforms.Uap.Presenters.Attributes;
using MvvmCross.ViewModels;

namespace ExampleApp.Uwp.Views
{
    public sealed partial class LibrariesView : IMvxBindingContextOwner
    {
        private IMvxInteraction<AppNotificationInteraction> _appNotificationInteraction;
        private IMvxBindingContext _bindingContext;

        public LibrariesView()
        {
            InitializeComponent();
        }

        public IMvxInteraction<AppNotificationInteraction> AppNotificationInteraction
        {
            get => _appNotificationInteraction;
            set
            {
                if (_appNotificationInteraction != null)
                {
                    _appNotificationInteraction.Requested -= AppNotificationInteractionOnRequested;
                }

                _appNotificationInteraction = value;
                _appNotificationInteraction.Requested += AppNotificationInteractionOnRequested;
            }
        }

        public IMvxBindingContext BindingContext
        {
            get => _bindingContext ?? (_bindingContext = new MvxBindingContext());
            set => _bindingContext = value;
        }

        public new LibrariesViewModel ViewModel => base.ViewModel as LibrariesViewModel;

        protected override void OnViewModelSet()
        {
            base.OnViewModelSet();

            BindingContext.DataContext = ViewModel;

            var set = this.CreateBindingSet<LibrariesView, LibrariesViewModel>();
            set.Bind(this).For(view => view.AppNotificationInteraction).To(viewModel => viewModel.AppNotificationInteraction).OneWay();
            set.Apply();
        }

        private void AppNotificationInteractionOnRequested(object sender, MvxValueEventArgs<AppNotificationInteraction> e)
        {
            AppNotification.Show(e.Value.Content);
        }
    }
}

Upvotes: 1

Related Questions