LoginSignup
0
0

More than 5 years have passed since last update.

Prism.FormsのNavigationService用のViewを一括登録する

Last updated at Posted at 2016-11-17

WPFではかずきさんのこの記事の
http://blog.okazuki.jp/entry/2014/09/11/224645
Bootstrapperの作成の項で書いてあるViewの一括登録を使ってたんですが
こんな感じでPrism.FormsのRegisterTypeForNavigationも一括でできないもんかなといろいろ試してみたらこんな感じで出来ました。

コード

protected override void RegisterTypes() {

    this.GetType().GetTypeInfo().Assembly
            .DefinedTypes
            .Where(t => t.Namespace?.EndsWith(".Views", System.StringComparison.Ordinal) ?? false)
            .ForEach(t => {
                Container.RegisterTypeForNavigation(t.AsType(), t.Name);
            });

}

この例では.Viewsで終わる名前空間の型を根こそぎ登録しているだけです。
別の場所に置いているならViewのある名前空間を指定するようにしてください。

追記

匿名型がある場合にNamespaceがnullになり、そこで落ちてしまう問題があったので以下のように修正しました。

.Where(t => t.Namespace.EndsWith(".Views", System.StringComparison.Ordinal))

.Where(t => t.Namespace?.EndsWith(".Views", System.StringComparison.Ordinal) ?? false)

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