既然我有一个外壳程序和几个使用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"))
                              });
              });
}

其他提示

要避免不必了解你的模块,并从伸手到以任何方式外壳的模块任何你的shell程序,我会提供这样你的模块接口:

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