2
votes

I'm writing monodroid app using Xamarin and MvvmCross.

I want to use both MvvmCross and ActionBar in my app. ActionBar (Legacy or Sherlock) forced me to inherit from their custom activity. In this case I couldn't inherit my Activity from both MvxActivity and CustomBarActivity.

How to show action bar withing MvxActivity?

1
@stuart, No Not duplicate - actionbarsherlock and actionbar are different - Tim

1 Answers

0
votes

What i did to impliment the Actionbar in a Mvx-like fashion, is i created a property in my TabActivity so i can access my ViewModel. In my ViewModel i have a list of actionbar elements i can enumerate and create. I'm sure there are more MvvMcross like ways to impliment an actionbar ( ask stuart ), but this works pretty fine for me. Don't forget to make your activity inherit from TabActivity.

Property:

protected MvvMAppiAppi.Core.ViewModels.NavigationBarViewModel NavigationBarViewModel {
            get{ return base.ViewModel as MvvMAppiAppi.Core.ViewModels.NavigationBarViewModel; }
        }

Code:

        protected override void OnCreate (Bundle bundle)
                {
                    base.OnCreate (bundle);
                    SetContentView (Resource.Layout.view_NavigationBar);

                    //TabHost.TabSpec spec;
                    Intent intent;

                    foreach (var item in NavigationBarViewModel.MenuTabs) {
                        switch (item.PresentationMode) {
                            case "listByCategory":
                        {
                            var vm = NavigationBarViewModel.ListPresentationViewModel;
                            vm.CategorieId = item.id;
                            intent = this.CreateIntentFor (vm);
                        }
                            break;
                     ...
                     ...
                     ...
                     ...
               }
    intent.AddFlags (ActivityFlags.NewTask);
    setUpTab (intent, item.Name, ResourceManager.I.getResId (item.ResourceName));

    }

Function to create the Tabs:

private void setUpTab (Intent view, string tag, int Resource)
        {
            View tabview = createTabView (this.TabHost.Context, tag, Resource);
            TabHost.TabSpec spec = this.TabHost.NewTabSpec (tag).SetIndicator (tabview).SetContent (view);

            this.TabHost.AddTab (spec);
        }

        private View createTabView (Context context, String text, int resource)
        {
            View view = LayoutInflater.From (context).Inflate (Resource.Layout.tabs_bg, null);
            TextView tv = view.FindViewById<TextView> (Resource.Id.tabsText);
            ImageView iv = view.FindViewById<ImageView> (Resource.Id.image);
            iv.SetImageResource (resource);
            tv.SetText (text, TextView.BufferType.Normal);
            return view;
        }