PushAsync from View Model

In my last post, we discussed about creating grid menu with help of Flow List View and how I navigated to other page from view model using Push Modal Async. Well, later I found out that method had one crucial weakness. When I tried it out in my iPhone simulator, after I chose one of the menus, I couldn’t go back to my previous page because in iPhone we don’t have back button. I did a further research on internet and came out with a solution.

Change the root page into navigation page

When I did the research, there was one keyword that always appeared, whether in Stack Overflow or Xamarin Forum, it was changing the root page into navigation page. It sound simple but it took me a while to apply it properly. And after some trial and errors, here’s what I finally did to my view model. Note: I only attach the updated parts and commented the original source to distinguish the different.

 

//private INavigation navigation;
private BreakfastPage breakfastPage;

public BreakfastViewModel(BreakfastPage breakfastPage)
{
  //this.navigation = navigation;
  this.breakfastPage = breakfastPage;
  BreakfastMenuList = new ObservableCollection<BreakfastMenu>()
  {
    new BreakfastMenu() { ImageSource = "Burger.png", MenuTitle = "BURGER" },
    new BreakfastMenu() { ImageSource = "Pizza.png", MenuTitle = "PIZZA" },
    new BreakfastMenu() { ImageSource = "EggBacon.png", MenuTitle = "BACON" },
    new BreakfastMenu() { ImageSource = "Sandwich.png", MenuTitle = "SANDWICH" },
  };
  MenuTappedCommand = new Command(async () => await MenuSelectedAsync());
}

private async Task MenuSelectedAsync()
{
  Application.Current.MainPage = new NavigationPage(breakfastPage);
  switch (SelectedBreakfastMenu.MenuTitle)
  {
    case "BURGER":
        await Application.Current.MainPage.Navigation.PushAsync(new BreakfastBurger());
        //await navigation.PushModalAsync(new BreakfastBurger());
        break;
    case "PIZZA":
        await Application.Current.MainPage.Navigation.PushAsync(new BreakfastPizza());
        //await navigation.PushModalAsync(new BreakfastPizza());
        break;
    case "BACON":
        await Application.Current.MainPage.Navigation.PushAsync(new BreakfastBacon());
        //await navigation.PushModalAsync(new BreakfastBacon());
        break;
    case "SANDWICH":
        await Application.Current.MainPage.Navigation.PushAsync(new BreakfastSandwich());
       //await navigation.PushModalAsync(new BreakfastSandwich());
       break;
  }

}

As you can see, I didn’t use INavigation anymore. Instead, I passed the content page through view model’s constructor then turn it into navigation page. Next, I set the Application’s current main page into my new navigation page and that enable me to call push async from view model. The following code is how I bind the view model to content page.

public partial class BreakfastPage : ContentPage
{
  public BreakfastPage()
  {
    InitializeComponent();
    BindingContext = new BreakfastViewModel(this);
  }
}

Hide Navigation Bar

If there’s one thing that I didn’t like about this method is the navigation bar. Turning my root page into navigation page causing it to have navigation bar, which I didn’t need. So, I also updated the xaml file of my menu by adding one line to hide navigation bar in content page.

<ContentPage NavigationPage.HasNavigationBar="False" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView" x:Class="Testing.Views.BreakfastPage">
  <ContentPage.Content ...>
</ContentPage>

Credit:

  • Icon from FlatIcon, Burger icon by Freepik, Pizza icon by¬†Smashicons, EggBacon and Sandwich Icon by Twitter
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s