- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经搜索了一些教程,甚至查看了pluralsite Introduction to PRISM。但是,大多数示例都基于使用统一容器,并且缺少有关如何使用 Mef 容器实现此功能的信息。
我的简单 helloworld 模块基于 web tutorial .我的代码是相同的,只是我只停留在 HelloModule 上并使用 Mef,而不是 Unity,如教程所示:
我的主要问题是如何用我的 View 模型初始化我的 View 。我通过实验发现的唯一工作方法是在 View 构造函数中初始化 View 模型:
HelloView.xaml.cs
namespace Hello.View
{
[Export]
public partial class HelloView : UserControl, IHelloView
{
public HelloView()
{
InitializeComponent();
Model = new HelloViewModel(this);
}
public IHelloViewModel Model
{
//get { return DataContext as IHelloViewModel; }
get { return (IHelloViewModel)DataContext; }
set { DataContext = value; }
}
}
}
[ModuleExport(typeof(HelloModule), InitializationMode=InitializationMode.WhenAvailable)]
public class HelloModule : IModule
{
IRegionManager _regionManager;
[ImportingConstructor]
public HelloModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void Initialize()
{
_regionManager.Regions[RegionNames.ContentRegion].Add(ServiceLocator.Current.GetInstance<HelloView>());
}
}
最佳答案
MatthiasG 展示了在 MEF 中定义模块的方法。请注意, View 本身并没有实现 IModule。但是,将 MEF 与 PRISM 结合使用的有趣部分是如何在启动时将模块导入 UI。
我只能在这里原则性地解释该系统,但它可能会为您指明正确的方向。每件事都有很多方法,但这是我理解的最佳实践,也是我在以下方面取得了非常好的经验:
自举
与 Prism 和 Unity 一样,这一切都始于 Bootstrapper,它源自 MefBootstrapper
在 Microsoft.Practices.Prism.MefExtensions
. Bootstrap 设置 MEF 容器并因此导入所有类型,包括服务、 View 、ViewModel 和模型。
导出 View (模块)
这是 MatthiasG 所指的部分。我的做法是 GUI 模块的以下结构:
[Export(typeof(MyModel)]
属性。用 [PartCreationPolicy(CreationPolicy.Shared)]
标记表示只创建一个实例(单例行为)。 [Export]
属性。这是一个例子:[ViewExport(RegionName = RegionNames.DataStorageRegion)]
public partial class DataStorageView
{
[ImportingConstructor]
public DataStorageView(DataStorageViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
}
[ViewExport]
属性做了两件事:因为它派生自
[Export]
属性,它告诉 MEF 容器导入 View 。作为什么?这隐藏在它的定义中:构造函数签名如下所示:
public ViewExportAttribute() : base(typeof(UserControl)) {}
[Export]
的构造函数类型为
UserControl
,每个 View 都注册为
UserControl
在 MEF 容器中。
RegionName
稍后将用于决定应将 View 插入 Shell UI 的哪个区域。 RegionName 属性是接口(interface)的唯一成员
IViewRegionRegistration
.属性类:
/// <summary>
/// Marks a UserControl for exporting it to a region with a specified name
/// </summary>
[Export(typeof(IViewRegionRegistration))]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[MetadataAttribute]
public sealed class ViewExportAttribute : ExportAttribute, IViewRegionRegistration
{
public ViewExportAttribute() : base(typeof(UserControl)) {}
/// <summary>
/// Name of the region to export the View to
/// </summary>
public string RegionName { get; set; }
}
AutoPopulateExportedViews
行为。这将使用以下行从 MEF 容器中导入所有模块:
[ImportMany]
private Lazy<UserControl, IViewRegionRegistration>[] _registeredViews;
UserControl
的所有类型来自容器,如果他们有一个元数据属性,它实现
IViewRegionRegistration
.因为您的
[ViewExport]
属性确实如此,这意味着您导入所有标有
[ViewExport(...)]
的类型.
OnAttach()
中执行此操作。属性(property):
/// <summary>
/// A behavior to add Views to specified regions, if the View has been exported (MEF) and provides metadata
/// of the type IViewRegionRegistration.
/// </summary>
[Export(typeof(AutoPopulateExportedViewsBehavior))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class AutoPopulateExportedViewsBehavior : RegionBehavior, IPartImportsSatisfiedNotification
{
protected override void OnAttach()
{
AddRegisteredViews();
}
public void OnImportsSatisfied()
{
AddRegisteredViews();
}
/// <summary>
/// Add View to region if requirements are met
/// </summary>
private void AddRegisteredViews()
{
if (Region == null) return;
foreach (var view in _registeredViews
.Where(v => v.Metadata.RegionName == Region.Name)
.Select(v => v.Value)
.Where(v => !Region.Views.Contains(v)))
Region.Add(view);
}
[ImportMany()]
private Lazy<UserControl, IViewRegionRegistration>[] _registeredViews;
}
.Where(v => v.Metadata.RegionName == Region.Name)
.这使用属性的 RegionName 属性来仅获取为特定区域导出的那些 View ,您要将行为附加到。
protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
{
ViewModelInjectionBehavior.RegionsToAttachTo.Add(RegionNames.ElementViewRegion);
var behaviorFactory = base.ConfigureDefaultRegionBehaviors();
behaviorFactory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof(AutoPopulateExportedViewsBehavior));
}
关于mvvm - 如何使用 PRISM 和 MEF 将 View 注入(inject) UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289178/
我有此消息的CompositionException异常: “类型为'SomeService'的ComposablePart不能重新组合,因为它处于无效状态。只有在已经完全预览或组合的情况下,才可以重
我目前正在调查 MEF,以了解我们公司正在编写的软件的某种插件子系统的可行性。我想到的第一个问题是了解 MEF 组件对应用程序加载时间的影响。 此外,如果这很重要,是否有可能在运行时完全加载这些组件?
我开始学习,MEF 和其中一件重要的事情是我可以用导出属性标记一些项目(类、属性、方法),以便任何想要使用它的人都会在实例变量上创建导入属性并使用它。这种映射是如何发生的,何时发生?导入是按需延迟发生
我知道 MEF CompositionContainer 创建并保留类的实例。我不知道在什么情况下 CompositionContainer 在其内部有一个类实例。 任何人都可以列出在 Composi
我需要向我们现有的代码添加一些扩展点,并且我一直将 MEF 视为一种可能的解决方案。我们有一个 IRandomNumberGenerator 接口(interface),带有一个我们希望可交换的默认实
我看到 MEF ( MEF Codeplex site ) 上的新功能仍在继续工作,并且他们正在发布 MEF 2 beta 版本,但是他们没有关于新功能正在试验/探索/构建的任何体面的文档? 那么,作
我有一个对象实例,并且我希望最终得到一个包含该对象实例并导出为特定接口(interface)类型的 MEF 目录。我怎样才能做到这一点? TypeCatalog在这里似乎不起作用,因为(a)它创建一个
我计划在 ASP.NET 中使用 MEF,以深入了解 CompositionContainer 的线程安全性。 我的第一种方法将不同的 CompositionContainer 与每个请求相关联,但我
这类似于我的代码的外观 var catalog = new AssemblyCatalog(typeof(Program).Assembly); _container = new Compositio
我正在使用 MEF 和 System.ComponentModel.Composition.dll 来加载一些 dll。 我正在做类似的事情: AggregateCatalog catalog = n
我认识到,MEF 仅限于 metro 风格的应用程序。没有容器了,那么我怎样才能获得特定的导出值,如 ILogger logger = container.GetExportedValues(); ?
据我所知,为了使导入/导出属性相互正确连接,我必须对具有这些属性的每个新实例调用 compositionContainerInstance.Compose(...)。 是否可以自动发生这种情况,这样我
所以不使用 ExportMetadata 属性,为什么不直接扩展接口(interface)呢? 最佳答案 如果您通过 MEF 导入 Lazy<> 内容,则无需实例化对象即可查看元数据。 关于mef -
我正在尝试将 signalR 集线器作为使用 MEF 的插件的一部分。但是在 List<> 对象上调用 ImportMany 然后在 Global.asax 文件的 Application_Start
我找不到它!? 最佳答案 ExportFactory不是桌面 MEF 的一部分(但是 - 截至目前,它仅针对 Silverlight 发布)。看起来它将包含在 MEF 2 中。 与此同时,Glenn
安装包含 MEF 组件的 VSIX 项目时,Visual Studio 如何查找 MEF 组件?我知道常规方法可能是添加包含 MEF 部分的 dll 目录的目录。 Visual Studio 如何知道
是否可以将 IronPython 类声明为“导出”,从而将它们添加到 MEF 目录中主机 C# 应用程序可以导入吗? 我真的找不到任何具体的例子,只是猜测。 以下是我如何手动加载实现 .NET 接口(
MEF 团队一直在说它是关于插件模型的。那么,我们是在谈论 UI 插件吗?我们如何在非 UI 代码中使用这些东西? 我下载了代码,示例都是关于 GUI 的。我猜错了吗? 最佳答案 伊利亚 MEF 绝对
我有一个选项卡式应用程序,我希望用户能够在其中搜索某个人,然后在新 View 中显示此人的详细信息。用户应该能够为不同的人打开多个人的详细信息 View 。 我有点不确定我是否遵循了创建新 View
在 What's new in .NET 4.5微软表示 MEF 现在将支持“多范围”。这是什么意思?我在任何地方都找不到关于它的文档。 最佳答案 我相信你的问题在这个博客中得到了完整的回答:http
我是一名优秀的程序员,十分优秀!