Skip to content

Instantly share code, notes, and snippets.



Last active Dec 26, 2015
What would you like to do?
Main view code for Android Cloud Auction example app, built with MvvmQuickCross
using Android.App;
using Android.Views;
using Android.OS;
using Android.Content.PM;
using MvvmQuickCross;
using CloudAuction.Shared;
using CloudAuction.Shared.ViewModels;
namespace CloudAuction
[Activity(Label = "Cloud Auction", MainLauncher = true, LaunchMode = LaunchMode.SingleTask, Icon = "@drawable/icon")]
public class MainView : ActivityViewBase<MainViewModel>
public static MainViewModel.SubView CurrentSubView;
private CloudAuctionApplication EnsureApplication()
return CloudAuctionApplication.Instance ?? new CloudAuctionApplication(new CloudAuctionNavigator());
protected override void OnCreate(Bundle bundle)
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
// One-time initialization of application and helpers.
// This can be done in the startup view, or in the android application object, or in the application loading screen.
CloudAuctionApplication.Instance.ContinueToMain(skipNavigation: true);
// Ensure that our viewmodel is initialized, in case we navigated to this view by some other means
// than through a call to CloudAuctionApplication.Instance.ContinueToMain().
// E.g. when the OS creates this view at application startup, because it is marked as MainLauncher.
ActionBar.Tab[] tabs = new ActionBar.Tab[] {
ActionBar.NewTab().SetText("Auction").SetTag(new Fragment()),
ActionBar.NewTab().SetText("Products").SetTag(new Fragment()),
ActionBar.NewTab().SetText("Help").SetTag(new Fragment()) };
foreach (var tab in tabs) tab.TabSelected += Tab_TabSelected;
Initialize(FindViewById(Resource.Id.MainView), CloudAuctionApplication.Instance.MainViewModel);
// This call initializes data binding and updates the view with the current viewmodel values.
for (int position = 0; position < tabs.Length; position++)
ActionBar.AddTab(tabs[position], setSelected: (MainViewModel.SubView)position == CurrentSubView);
private void EnsureCurrentTabIsSelected()
int index = (int)CurrentSubView;
if (ActionBar.SelectedNavigationIndex != index)
protected override void OnResume()
private void Tab_TabSelected(object sender, ActionBar.TabEventArgs e)
var tab = (ActionBar.Tab)sender;
e.FragmentTransaction.Replace(Resource.Id.MainView_TabFragmentContainer, (Fragment)tab.Tag);
// TODO: Check if we should also use .Remove in TabUnselected event? E.g. see
CurrentSubView = (MainViewModel.SubView)tab.Position;
public override bool OnCreateOptionsMenu(IMenu menu)
MenuInflater.Inflate(Resource.Menu.MainMenu, menu);
return base.OnPrepareOptionsMenu(menu);
public override bool OnOptionsItemSelected(IMenuItem item)
switch (item.ItemId)
case Resource.Id.LogoutMenuItem: ViewModel.LogoutCommand.Execute(null); return true;
default: return base.OnOptionsItemSelected(item);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment