我开发了一个使用MEF的应用程序,可以在表单上显示所有可用的UserControl。用户控件和表单都驻留在同一个程序集中。当我从XP启动exe时,这一切都正常,但在使用Windows 7机器时抛出异常。是否有任何建议可以解决这个问题。
答案 0 :(得分:1)
我的第一个建议是展示你的构图方法和一些代码示例。否则,我会消除除UserControl
之外的所有负载。从那里开始。请确保你:
[Export(typeof(IUserControl))]
public class myUserControl : UserControl, IUserControl
{
...
/*
* control to be exported:
* note: you can forego IUserControl and just use UserControl
* but make sure you do so throughout the import and
* export attributes.
*/
...
}
...然后在主机应用中:
[ImportMany(typeof(IUserControl))]
IEnumerable<IUserControl> UserControls {get;}
我在这里使用IEnumerable作为一个例子,因为你期望加载几个UserControls。我假设您将加载要立即显示的控件。否则,如果你不是一次性想要它们,而是 on demand ,我仍然会这样列举:
[ImportMany(typeof(IUserControl))]
IEnumerable<Lazy<IUserControl>> UserControls {get;}
这样你就可以迭代,测试UserControls[index].Value
为null。
如果没有更多信息,这是我能为您做的最好的事情。
答案 1 :(得分:0)
HI,
我解决了这个问题。我在应用程序中使用Log4Net,由于一些奇怪的原因,Winforms的安装应用程序没有采用log4not xml文件。安装版本中缺少这个,这就是应用程序出错的原因。
感谢您的回复。