コンポジットWPF(プリズム)モジュールのリソースデータテンプレート

StackOverflow https://stackoverflow.com/questions/1172171

  •  19-09-2019
  •  | 
  •  

質問

...私はシェルアプリケーションとMicrosoft CompoisteWPF(プリズムV2)を使用して、別のモジュール・プロジェクトのカップルを持っていることを考えると

コマンドを受信すると、モジュールは、新しいビューモデルを作成し、領域マネージャを介して領域に追加します。

var viewModel = _container.Resolve<IMyViewModel>();
_regionManager.Regions[RegionNames.ShellMainRegion].Add(viewModel);

私は、モジュール内のリソースの辞書を作成し、(XAML下記参照)にロードされたビューモデルタイプのビューを表示するには、データテンプレートを設定することができると考えました。ビューモデルをビューに追加された場合でも、私が得るすべては、プリントアウトし、ビューモデルの名前空間です。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:Modules.Module1.ViewModels"
    xmlns:vw="clr-namespace:Modules.Module1.Views"
>
    <DataTemplate DataType="{x:Type vm:MyViewModel}">
        <vw:MyView />
    </DataTemplate>
</ResourceDictionary>

編集ます:

私はそれがApp.xaml

を追加することによって、仕事を得ることができます
<Application.Resources>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Module1;component/Module1Resources.xaml"/>
        <ResourceDictionary Source="pack://application:,,,/Module2;component/Module2Resources.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</Application.Resources>

結構ですが、それは新しいモジュールが作成されると、App.xamlファイルが追加する必要があることを意味します。彼らは動的Application.Resourcesに追加する負荷として私が探している、モジュールの方法です。これは可能でしょうか?

役に立ちましたか?

解決 2

各モジュールの初期化の中で、あなたは、アプリケーションのリソースに追加することができます:

Application.Current.Resources.MergedDictionaries
                .Add(new ResourceDictionary
                {
                    Source = new Uri(
                        @"pack://application:,,,/MyApplication.Modules.Module1.Module1Init;component/Resources.xaml")
                });

それとも、各モジュールの慣例に従えば、「Resources.xmal」と呼ばれるリソースディクショナリを持っている...

protected override IModuleCatalog GetModuleCatalog()
{
    var catalog = new ModuleCatalog();

    AddModules(catalog,
               typeof (Module1),
               typeof(Module2),
               typeof(Module3),
               typeof(Module4));

    return catalog;
}

private static void AddModules(ModuleCatalog moduleCatalog,
    params Type[] types)
{
    types.ToList()
         .ForEach(x =>
             {
                 moduleCatalog.AddModule(x);
                 Application.Current.Resources.MergedDictionaries
                     .Add(new ResourceDictionary
                              {
                                  Source = new Uri(string.Format(
                                                       @"pack://application:,,,/{0};component/{1}",
                                                       x.Assembly,
                                                       "Resources.xaml"))
                              });
              });
}

他のヒント

どのような方法でシェルに手を差し伸べるからあなたのモジュールとあなたのモジュールについて何を知っていることから、あなたのシェルアプリを避けるために、私はこのようなあなたのモジュールへのインタフェースを提供したい:

IMergeDictionaryRegistry
{
     void AddDictionaryResource(Uri packUri);
}

あなたはモジュールのコードでは、このインターフェイスをお願いしたい。

public class MyModule : IModule
{
     IMergeDictionaryRegistry _merger;
     public MyModule(IMergeDictionaryRegistry merger)
     {
          _merger = merger;
     }

     public void Initialize()
     {
          _merger.AddDictionaryResource(new Uri("pack://application:,,,/Module1;component/Module1Resources.xaml");
     }
}

あなたはこれを行うには、あなたのシェルでこれを実装します:

public MergeDictionaryRegistry : IMergeDictionaryRegistry
{
     public void AddDictionaryResource(Uri packUri)
     {
          Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
          {
               Source = packUri;
          });
     }
}

そして最後に、あなたのブートストラップのConfigureContainerでます:

public override void ConfigureContainer()
{
     base.ConfigureContainer();
     Container.RegisterType<IMergeDictionaryRegistry, MergeDictionaryRegistry>();
}

これはあなたがの必要な機能を取得し、はシェルと、あなたのモジュールは、互いに独立して残ります。これは、あなたのモジュールのコードをテストするためにApplicationをスピンアップする必要がありません(ただモックIMergeDictionaryRegistryを、あなたが行われている)という点で、より多くのテスト可能であることの付加的な利点を持っています。

私たちは、これはあなたのために行く方法を知ってみましょう。

すべては多くの作業のように思えること!

個人的に、私はちょうどこのように私のビューのUserControl.Resourcesセクション内のリソースディクショナリを宣言...

<UserControl.Resources>
    <ResourceDictionary Source="../Resources/MergedResources.xaml" />
</UserControl.Resources>

私は含める必要が任意のリソースを指し、その後辞書をマージします。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Iconography.xaml" />
    <ResourceDictionary Source="Typeography.xaml" />
</ResourceDictionary.MergedDictionaries>

あなたはそこに私が推測するに、あなたのデータテンプレートを宣言すると思います。

HTHます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top