3

I'm using MvvmCross and SherlockActionBar. My problem is that I need to make item in SherlockActionBar dissapear depending on value of some ViewModel property.

Item in the actionBar have property IsVisible but unfortunately it doesn't have a setter (you need to set visibility by item.SetVisible(boolValue) ) so I decided to make my own property ItemVisible in View.cs (binding it to the ViewModel-property) which will on change call item.SetVisible.

I've searched how to do in code bindings and found this.

So I bind ItemVisible View property to ViewModelProperty but it never stepped into ItemVisible setter. Of course I raise RaiseAllPropertyChanged in viewModel after ViewModelProperty could be changed. I've looked into mvvmcross bindings but I didn't find answer for my problem. What am I doing wrong?

    public class SomeView : BaseActionBarActivity {
    private IMenuItem _item ;

    private bool ItemVisible
    {
        get { return _item.IsVisible; }
        set { _item.SetVisible(value); }
    }

    protected override void OnCreate(Bundle bundle)
    {
        SetTheme(Resource.Style.Theme_Sherlock);
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.SomeView);

        var set = this.CreateBindingSet<SomeView, SomeViewModel>();
        set.Bind(this).For(p => p.ItemVisible).To(e => e.ViewModelProperty);
        set.Apply();
    }

    public override bool OnCreateOptionsMenu(Xamarin.ActionbarSherlockBinding.Views.IMenu menu)
    {
        SupportMenuInflater.Inflate(Resource.Menu.SomeMenu, menu);

        _item = menu.FindItem(Resource.Id.xmlMenuResource);
    }}
Community
  • 1
  • 1
ozapa
  • 281
  • 4
  • 16

1 Answers1

2

I'd guess this is due to the private in private bool ItemVisible - MvvmCross needs to use reflection to call this member and it's hard to do this on private members due to CLR security restrictions.

Try:

public bool ItemVisible
{
    get { return _item.IsVisible; }
    set { _item.SetVisible(value); }
}

This topic is also covered a little in N=18 and N=28 in http://mvvmcross.blogspot.co.uk/ (and was also covered in my NDC London talk on https://speakerdeck.com/cirrious/data-bind-everything but I'm afraid that hasn't been video recorded yet!)

Stuart
  • 66,722
  • 7
  • 114
  • 165