我正在使用Unity作为我的IoC容器,在我的引导程序中我有这个代码:
protected override IModuleCatalog CreateModuleCatalog()
{
return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
new Uri("modulecatalog.xaml", UriKind.Relative));
}
我创建了一个名为“modulecatalog.xaml”的xml文件,其中包含:
<?xml version="1.0" encoding="utf-8" ?>
<Modularity:ModuleCatalog
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
</Modularity:ModuleCatalog>
我已经在xaml中收到错误,说无法找到Microsoft.Practices.Prism.Modularity而且找不到Modularity:ModuleCatalog。
这非常令人沮丧。我的Microsoft.Practices.Prism包含在我的项目中。我的Bootstrapper中的代码编译得如此明显,Microsoft.Practices.Prism.Modularity.ModuleCatalog确实存在。但更奇怪的是,如果我将CreateModuleCatalog函数更改为:
using Microsoft.Practices.Prism.Modularity;
...
protected override IModuleCatalog CreateModuleCatalog()
{
return ModuleCatalog.CreateFromXaml(
new Uri("modulecatalog.xaml", UriKind.Relative));
}
它说ModuleCatalog上不存在CreateFromXaml。找到ModuleCatalog没有问题,但是如果我每次都没有输入完整的命名空间,那么该功能显然不存在。 WTF还在继续?
答案 0 :(得分:0)
尝试显式解析ModuleCatalogtype,它应该可以正常工作
protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
{
return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml( new Uri( "Modules.xaml", UriKind.Relative ) );
}
未找到模块性的错误:ModuleCatalog对我来说是相同的,但应用程序运行良好。
啊,我差点忘了。将modules.xaml文件设置为resource答案 1 :(得分:0)
我也很努力。我最终通过以下方式解决了这个问题:
<强>设置:强> 在您的shell中,您应该定义您的区域:
<Window x:Class="Prism101.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://www.codeplex.com/prism"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot" Background="White">
<ContentControl prism:RegionManager.RegionName="MainContent" />
</Grid>
</Window>
步骤1:添加对模块项目/程序集的引用。
步骤2:在模块目录中声明模块: 这将在您的模块项目中。
<Modularity:ModuleCatalog
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism.Composition">
<Modularity:ModuleInfo
Ref="Prism101.Modules.Core.xap"
ModuleName="CoreModule"
ModuleType="Prism101.Modules.Core.CoreModule, Prism101.Modules.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</Modularity:ModuleCatalog>
第3步:实施您的模块 请注意框架将如何为您初始化RegionManager和UnityContainer。 还要注意&#34; WelcomeView&#34;是一个usercontrol,它被映射到客户端shell的maincontent区域。
namespace Prism101.Modules.Core
{
public class CoreModule : IModule
{
private readonly IUnityContainer container;
private readonly IRegionManager regionManager;
public CoreModule(IUnityContainer container, IRegionManager regionManager)
{
this.container = container;
this.regionManager = regionManager;
}
public void Initialize()
{
var view = new WelcomeView();
regionManager.AddToRegion("MainContent", view);
}
}
}
第4步:实施Bootstrapper: 在CreateModuleCatalog方法中,标识目录模块所在的程序集的名称及其目录路径。
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
var shell = new MainWindow();
Application.Current.MainWindow = shell;
return shell;
}
protected override IModuleCatalog CreateModuleCatalog()
{
var assemblyName = "Prism101.Modules.Core";
var filePath = "ModuleCatalog.xaml";
var uriPath = string.Format("/{0};component/{1}", assemblyName, filePath);
var uri = new Uri(uriPath, UriKind.Relative);
var moduleCatalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(uri);
return moduleCatalog;
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow.Show();
}
}
<强>结论:强> 在查看这些步骤之后,应该能够运行应用程序并观察在加载模块的情况下引导prism应用程序的结果。
注意:强> 请随时提供反馈,告诉我如何更好地表达这一点。