- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个 UserControl,它使用 [Import]
属性导入多个部分。
public class MyUserControl : UserControl, IPartImportsSatisfiedNotification
{
[Import]
public IService Service { get; set; }
public MyUserControl()
{
}
public void OnImportsSatisfied()
{
// Do something with Service.
}
}
此 UserControl 是从 XAML 实例化的,因此它的导入未得到满足并且 OnImportsSatisfied
未被调用。
<local:MyUserControl />
我的问题是在 XAML 中创建我的类时如何满足它的导入。
最佳答案
来自 MSDN:
To be instantiated as an object element in XAML, a custom class must meet the following requirements:
The custom class must be public and must expose a default (parameterless) public constructor. (See following section for notes regarding structures.)
The custom class must not be a nested class. The extra "dot" in the full-name path makes the class-namespace division ambiguous, and interferes with other XAML features such as attached properties.
If an object can be instantiated as an object element, the created object can fill the property element form of any properties that take the object as their underlying type.
You can still provide object values for types that do not meet these criteria, if you enable a value converter. For more information, see Type Converters and Markup Extensions for XAML.
从那里,你有两个选择:
1) 使用 TypeConverter :
使用类型转换器将允许您在没有无参数构造函数的情况下实例化一个对象,但您必须提供一个将执行实例化的 TypeConverter。
现在,我从来没有用过它,我无法在这方面进一步帮助你。
2) 使用 ServiceLocator 检索 IService:
public class MyUserControl : UserControl
{
public IService Service { get; set; }
public MyUserControl()
{
Service = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<IService>();
// You can do something with Service here already.
}
}
我知道这是你类(class)设计的改变,但希望你能应付。
希望对您有所帮助,
宝贝。
关于c# - 在从 XAML 实例化的控件中使用 MEF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667983/
我有此消息的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
我是一名优秀的程序员,十分优秀!