以下の内容はhttps://shuhelohelo.hatenablog.com/entry/2020/06/09/112100より取得しました。


Xamarin.FormsでASP.NET CoreなDI(Dependency Injection) (4)

NavigationPageを使ったページ遷移をやってみようと思います.

変更するところはまず,Appクラスのコンストラクタです.

以下のようにNavigationPageをはさみます.

        public App(MainPage mainPage)
        {
            InitializeComponent();
            
            MainPage = new NavigationPage(mainPage);
        }

新しくSecondPageを作って,MainPageから遷移するとしましょう.

そうすると,MainPageからはボタンを押したときにMainPageViewModel経由でPushAsyncメソッドで遷移するという感じでしょうか.

すると以下のようにServiceProviderからインスタンスを取得する?




もしくは,コンストラクタでSecondPageのインスタンスを受け取ってそれを使うとか.

        public MainPageViewModel(IDataService dataService, INotificationService notificationService, SecondPage secondPage)
        {
...省略

            NavigateToSecondPageCommand = new Command(_ =>
            {
                //App.Current.MainPage.Navigation.PushAsync(Startup.ServiceProvider.GetService<SecondPage>());
                App.Current.MainPage.Navigation.PushAsync(secondPage);
            });
        }

ページ遷移時のパラメータの受け渡しはどうするんだ?

ShellだとQueryPropertyとしてコードビハインド側で受け取れるから,それをViewModelに詰め直すという感じでいいのかな.

もしくは,グローバルなDictionaryを用意して,そこに入れるか.

ViewModel側でコードビハインドのときと同様にQueryProperty属性を使って,クエリーパラメータの値をプロパティに格納することができる.

ソースコード

github.com




以上の内容はhttps://shuhelohelo.hatenablog.com/entry/2020/06/09/112100より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14