0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Prism コードサンプル学習:19-NavigationParticipation

Posted at

Prism コードサンプル学習:19-NavigationParticipation

はじめに

以下の記事の続きです。
https://qiita.com/mngreen/items/69d5f993b28d629db896

19-NavigationParticipation

本サンプルは、ボタンを押下するとタブコントロールにタブアイテムが追加されるサンプルです。
ボタンを押下するごとにタブのビューのカウントがインクリメントされます。
以下のINavigationAware.OnNavigateToメソッドを実装することで、UIの追加および遷移時にカウントがインクリメントされる処理を実装されています。

using Prism.Mvvm;
using Prism.Regions;
namespace ModuleA.ViewModels
{
    public class ViewAViewModel : BindableBase, INavigationAware
    {
        private string _title = "ViewA";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private int _pageViews;
        public int PageViews
        {
            get { return _pageViews; }
            set { SetProperty(ref _pageViews, value); }
        }

        public ViewAViewModel()
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            PageViews++;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
    }
}

ユーザーがビューを追加するために必要な処理はとくになく、過去の記事で見た通りにIRegionManager.RequestNavigateメソッドを用いること、そしてTabControlにRegionManagerを用いて名前を与えることです。
今回のサンプルの変化点としては、VMでINavigationAwareインターフェイスを実現しているため、この処理MvvmHelpersの処理が実行されている点が異なるところです。
実際にUI要素のDataContextからVMを取得し、実行されるということがわかります。

おわりに

今回は、RegionManager.RequestNavigateメソッドを利用し、タブアイテムが増えていくサンプルを見ていきました。
ただ、大きな流れ(クラス間の関連やメソッド呼び出し)はあまり大きく変わってはいませんでしたが、INavigationAwareインターフェイスを実現することでビュー遷移後に処理が実行される仕組みを読み取ることができました。
次回、20-NavigateToExistingViewsについて見ていこうと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?