Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use actionbar with Xamarin and MvvmCross

I've seen Xamarin recently released as component http://components.xamarin.com/view/xamandroidsupportv7appcompat

I would like to use it with MvvmCross in my app, but I ran into difficulties, so I turned to google and found thistutorial http://blog.ostebaronen.dk/2013/11/getting-support-v7-working-with.html

I don't understand how to use it correctly with MvvmCross though. How can I bind buttons in it to ICommands etc. ? Can I specify whole actionbar as ViewModel and bind to it ? If so, how ?

like image 733
KadekM Avatar asked Dec 26 '13 19:12

KadekM


1 Answers

There is no way to bind ActionBar to ICommands with MvvmCross. But you can use a simple trick and call into your ICommands from the activity when a button on ActionBar is pressed.

public override bool OnCreateOptionsMenu(IMenu menu)
{
    this.MenuInflater.Inflate(Resource.Menu.QuoteDetails, menu);

    m_MenuItem_EditQuote = menu.FindItem(Resource.Id.menu_EditQuote);
    m_MenuItem_EditQuote.SetVisible(ViewModel.CanEdit);

    return true;
}

public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {

        case Resource.Id.menu_EditQuote:

            ViewModel.EditQuoteCommand.Execute(null);
            return true;

        case Resource.Id.menu_ViewQuote:

            ViewModel.DownloadQuoteCommand.Execute(null);
            return true;

        case Resource.Id.menu_EmailQuote:

            ViewModel.EmailQuoteCommand.Execute(null);
            return true;

        default:
            return base.OnOptionsItemSelected(item);
    }
}
like image 120
Alexey Avatar answered Sep 27 '22 22:09

Alexey