MVVMでのページ遷移（Android）

ページの遷移ってみんなどうしてるでしょうか？
頭がこんがらがってできたコードをさらす。


```
using Xamarin.Forms;

namespace Test
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new CustomNavigationPage(new Social.MainPage());
        }

        protected override void OnStart()
        {
            // Handle when your app start
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}
```

```
using System.Diagnostics;
using Xamarin.Forms;
namespace Test
{
    public class CustomNavigationPage : NavigationPage
    {
        public CustomNavigationPage(Page root) : base(root)
        {
            Pushed += (sender, e) => PushudExpansion(sender, e);
            Popped += (sender, e) => PoppedExpansion(sender, e);
        }


        protected override void OnAppearing()
        {
            ScreenTransition.BaseNavigation = ((CustomNavigationPage)this).CurrentPage.Navigation;
            Debug.WriteLine(((CustomNavigationPage)this).CurrentPage.ToString());
        }
        private void PoppedExpansion(object sender, NavigationEventArgs e)
        {
            ScreenTransition.ActiveNavigation = ((CustomNavigationPage)sender).CurrentPage.Navigation;
            Debug.WriteLine(((CustomNavigationPage)sender).CurrentPage.ToString());
        }

        private void PushudExpansion(object sender, NavigationEventArgs e)
        {
            ScreenTransition.ActiveNavigation = ((CustomNavigationPage)sender).CurrentPage.Navigation;
            Debug.WriteLine(((CustomNavigationPage)sender).CurrentPage.ToString());
        }
    }
}
```

```
using Xamarin.Forms;
namespace Test
{
    public class ScreenTransition 
    {
        private static INavigation _navi;
        private static INavigation _base;

        public static INavigation ActiveNavigation
        {
            private get { return _navi; }
            set
            {
               _navi = value;
            }
        }

        public static INavigation BaseNavigation
        {
            private get { return _base; }
            set
            {
                _base = value;
                ActiveNavigation = _base;
            }
        }

        public static void NewPushPage(Page page)
        {
            var navi = page;
            ActiveNavigation.PushAsync(navi);
            ActiveNavigation = navi.Navigation;
        }

        public static void NewPushModalPage(Page page)
        {
            var navi = new CustomNavigationPage(page);
            ActiveNavigation.PushModalAsync(navi);
            ActiveNavigation = navi.Navigation;
        }

        public static void PopToRoot()
        {
            ActiveNavigation.PopToRootAsync();
            ActiveNavigation = _base;
        }

        public static void PopModal()
        {
            ActiveNavigation.PopModalAsync();
        }

        public static void Pop()
        {
            ActiveNavigation.PopAsync();
        }

        public static void InitializeAllPages()
        {
            App.Current.MainPage = new CustomNavigationPage(new Social.MainPage());
        }
    }
}

```
```
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms;

namespace Test
{
    class ViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ICommand MessageCommand { get; set; }
        public INavigation _navi { get; set; }
        private string name = "Hello World";

        public ViewModel()
        {
            MessageCommand = new Command<string>(TestClickEvent);
        }

        public string Name
        {
            get { return this.name; }
            set
            {
                this.name = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
            }
        }

        private void TestClickEvent(string obj)
        {
            this.Name = "Click";
            var page = new Page1();
            ScreenTransition.NewPushModalPage(page);
        }
    }
}

```

```
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Test"
             x:Class="Test.MainPage">
  <ContentPage.BindingContext>
      <local:ViewModel></local:ViewModel>
  </ContentPage.BindingContext>
  <StackLayout>
  <Label Text="{Binding Name}"
           VerticalOptions="Center"
           HorizontalOptions="Center" x:Name="mylabel"/>
    <Button Text="click" Command="{Binding MessageCommand}" CommandParameter="{Binding Name}"/>
  </StackLayout>

</ContentPage>
```

```
using Xamarin.Forms;

namespace Test{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
}

```
最後まで見てくれた方、ありがとうございました。
御意見等をお待ちしております。