Question

i have a little problem with the binding on code of a fragment with MvvMCross in Xamarin. First of all what I want to do or what I Do: I hope my explanation is okay. I start with the lowest element. First, I build my UI dynamic. I have a lot of “templates” .They define my fragment-snips for example a Lable and an ACTextEdit-Field.

Then I have a lot of Fragments (my Views). They contain this codesnippes In a separate class I add the snips to the Fragment and give them unique id’s My ground is the Tutorial for using Fragments and ViewPager with Mvx. Of Ostebaron:

http://blog.ostebaronen.dk/2013/07/fragments-and-viewpager-with-mvx.html

and now I have to create a Bindingset with my Fragement and my VieModel. And then I have to bind the Elements with the properties in the View Model. But i don’t know on which place In the code. I tried to do it in the HomeView, in a OncreateView function but, it did not work.

Does anybody know how to solve this problem or give me some advice about it?

i choos this for binding:

using Android.OS;
using Android.Views;
using Android.Widget;
using Cirrious.MvvmCross.Binding.BindingContext;
using Cirrious.MvvmCross.Binding.Droid.BindingContext;
using Cirrious.MvvmCross.Droid.Fragging.Fragments;
using TipCalc.Core.ViewModels;

namespace TipCalc.UI.Droid.Views.Fragments
{

    public class TipCalcFragment : MvxFragment
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
           View v = inflater.Inflate(Resource.Layout.view_tip, container, false);
            var edit = v.FindViewById<TextView>(Resource.Id.editTest43);
            var ignore = base.OnCreateView(inflater, container, savedInstanceState);
            var set = this.CreateBindingSet<TipCalcFragment, TipViewModel>();
            set.Bind(edit).To(vm => vm.Tip);
            set.Apply();
            return this.BindingInflate(Resource.Layout.view_tip, null);
        }
    }
}

and it dosn't work maybe the wrong place?

Was it helpful?

Solution

I strongly recommend that you declare your binding inside your layout file. Then your OnCreateView can be reduced to this:

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    this.EnsureBindingContextIsSet (savedInstanceState);
    return this.BindingInflate(Resource.Layout.UserHeaderView, null);
}

Even without declaring it in your layout file, you should always call EnsureBindingContextIsSet. It is an extension method, so you might have to declare

  • Cirrious.MvvmCross.Binding.BindingContext
  • Cirrious.MvvmCross.Binding.Droid.BindingContext

in your usings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top