棱镜MEF——导入服务
我正在尝试弄清楚如何正确地将服务导入到我的 ViewModel 中...这是我的相关代码(我省略了不重要的内容):
ClientBootstrapper.cs:
public sealed class ClientBootstrapper : MefBootstrapper
{
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
//Add the executing assembly to the catalog.
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
}
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<ClientShell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
ClientShell.xaml .cs:
[Export()]
public partial class ClientShell : Window
{
[Import()]
public ClientViewModel ViewModel
{
get
{
return DataContext as ClientViewModel;
}
private set
{
DataContext = value;
}
}
public ClientShell()
{
InitializeComponent();
}
}
ClientViewModel.cs:
[Export()]
public class ClientViewModel : NotificationObject, IPartImportsSatisfiedNotification
{
[Import()]
private static RandomService Random { get; set; }
public Int32 RandomNumber
{
get { return Random.Next(); } //(2) Then this throws a Null Exception!
}
public void OnImportsSatisfied()
{
Console.WriteLine("{0}: IMPORTS SATISFIED", this.ToString()); //(1)This shows up
}
}
RandomService.cs:
[Export()]
public sealed class RandomService
{
private static Random _random = new Random(DateTime.Now.Millisecond);
public Int32 Next()
{
return _random.Next(0, 1000);
}
}
I do get the notification that all the import parts have been satisfied (1), but then I get a NullReferenceException (2) on
return Random.Next();
inside of the ClientViewModel. Not sure why I would get a NullReferenceException after I'm told that all Imports are satisfied...I'm trying to figure out how to import a service into my ViewModel properly... here's my relevant code (I've omitted the unimportant stuff):
ClientBootstrapper.cs:
public sealed class ClientBootstrapper : MefBootstrapper
{
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
//Add the executing assembly to the catalog.
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
}
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<ClientShell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
ClientShell.xaml.cs:
[Export()]
public partial class ClientShell : Window
{
[Import()]
public ClientViewModel ViewModel
{
get
{
return DataContext as ClientViewModel;
}
private set
{
DataContext = value;
}
}
public ClientShell()
{
InitializeComponent();
}
}
ClientViewModel.cs:
[Export()]
public class ClientViewModel : NotificationObject, IPartImportsSatisfiedNotification
{
[Import()]
private static RandomService Random { get; set; }
public Int32 RandomNumber
{
get { return Random.Next(); } //(2) Then this throws a Null Exception!
}
public void OnImportsSatisfied()
{
Console.WriteLine("{0}: IMPORTS SATISFIED", this.ToString()); //(1)This shows up
}
}
RandomService.cs:
[Export()]
public sealed class RandomService
{
private static Random _random = new Random(DateTime.Now.Millisecond);
public Int32 Next()
{
return _random.Next(0, 1000);
}
}
I do get the notification that all the import parts have been satisfied (1), but then I get a NullReferenceException (2) on
return Random.Next();
inside of the ClientViewModel. Not sure why I would get a NullReferenceException after I'm told that all Imports are satisfied...如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MEF 无法满足静态属性的导入。使随机服务成为实例属性。
MEF won't satisfy imports on a static property. Make the random service an instance property.
您可以使用
[ImportingConstructor]
并在构造函数中设置静态属性。只是不要将其设置为静态字段。
You can use
[ImportingConstructor]
and set the static property in the constructor.Just don't set it to a static field.