我将Unity用于我的IoC容器,并且在我的bootstrapper中有以下代码:
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,也找不到Modulation.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是怎么回事?
发布于 2012-09-24 01:31:21
尝试像这样显式解析ModuleCatalogtype,它应该可以工作
protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
{
return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml( new Uri( "Modules.xaml", UriKind.Relative ) );
}
not found The Modularity:ModuleCatalog的错误对我来说是相同的,但应用程序运行得很好。
啊,我差点儿忘了。将modules.xaml文件设置为资源
发布于 2014-08-02 23:01:34
我也为此而苦苦挣扎。我最终通过以下方式解决了这个问题:
设置:在您的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。还要注意,"WelcomeView“是一个映射到客户端外壳的主要内容区域的用户控件。
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步:实现引导程序:在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();
}
}
结论:在回顾了这些步骤之后,人们应该能够运行应用程序,并观察引导带有加载模块的棱镜应用程序的结果。
备注:请随时就我如何更好地表达这一点提供反馈。
https://stackoverflow.com/questions/12550585
复制相似问题