C# 如何在MVVM应用程序中将EventAggregator注册为单例?
构建我的第一个MVVM应用程序,它引入了一些非常强大的概念,但同时需要立即学习很多东西。我现在遇到的问题是,当我从另一个ViewModel发布事件时,事件订阅者似乎没有收到这些事件 我想我需要C# 如何在MVVM应用程序中将EventAggregator注册为单例?,c#,dependency-injection,prism,autofac,eventaggregator,C#,Dependency Injection,Prism,Autofac,Eventaggregator,构建我的第一个MVVM应用程序,它引入了一些非常强大的概念,但同时需要立即学习很多东西。我现在遇到的问题是,当我从另一个ViewModel发布事件时,事件订阅者似乎没有收到这些事件 我想我需要new在App.xaml.cs或我的BootStrapper类中创建一个EventAggregator,然后将该实例注入需要引用它的每个ViewModel中。我认为正在发生的是,正在为每个视图模型创建一个新的IEventAggregator,并且我正在发布/子发布到不同的实例。不确定此处是否与EventAg
new
在App.xaml.cs或我的BootStrapper
类中创建一个EventAggregator,然后将该实例注入需要引用它的每个ViewModel中。我认为正在发生的是,正在为每个视图模型创建一个新的IEventAggregator
,并且我正在发布/子发布到不同的实例。不确定此处是否与EventAggregator模式/Prism或DI/Autofac断开连接
我是否应该这样做:
public partial class App : Application
{
//Add this...
IEventAggregator eventAggregator = new EventAggregator();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//... and pass in here somehow?
var window = new BootStrapper()
.Bootstrap()
.Resolve<AView>();
window.Show();
}
}
public class BootStrapper
{
//Add this...
IEventAggregator eventAggregator = new EventAggregator();
public IContainer Bootstrap()
{
var builder = new ContainerBuilder();
builder.RegisterType<AView>()
.AsSelf();
builder.RegisterType<AViewModel>()
.AsSelf();
builder.RegisterType<OtherViewModel>()
.As<IOtherViewModel>();
builder.RegisterType<ADataProvider>()
.As<IADataProvider>();
builder.RegisterType<ADataService>()
.As<IDataService<Account>>();
//What I'm doing now
builder.RegisterType<EventAggregator>()
.As<IEventAggregator>();
//...and register instance here?
builder.RegisterType<AccountSelectedEvent>()
.AsSelf();
return builder.Build();
}
公共部分类应用程序:应用程序
{
//加上这个。。。
IEventAggregator eventAggregator=新的eventAggregator();
启动时受保护的覆盖无效(StartupEventArgs e)
{
基础。启动时(e);
//…不知怎么从这里经过?
var window=new BootStrapper()
.Bootstrap()
.Resolve();
window.Show();
}
}
或者像这样:
public partial class App : Application
{
//Add this...
IEventAggregator eventAggregator = new EventAggregator();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//... and pass in here somehow?
var window = new BootStrapper()
.Bootstrap()
.Resolve<AView>();
window.Show();
}
}
public class BootStrapper
{
//Add this...
IEventAggregator eventAggregator = new EventAggregator();
public IContainer Bootstrap()
{
var builder = new ContainerBuilder();
builder.RegisterType<AView>()
.AsSelf();
builder.RegisterType<AViewModel>()
.AsSelf();
builder.RegisterType<OtherViewModel>()
.As<IOtherViewModel>();
builder.RegisterType<ADataProvider>()
.As<IADataProvider>();
builder.RegisterType<ADataService>()
.As<IDataService<Account>>();
//What I'm doing now
builder.RegisterType<EventAggregator>()
.As<IEventAggregator>();
//...and register instance here?
builder.RegisterType<AccountSelectedEvent>()
.AsSelf();
return builder.Build();
}
公共类引导程序
{
//加上这个。。。
IEventAggregator eventAggregator=新的eventAggregator();
公共IContainer引导程序()
{
var builder=new ContainerBuilder();
builder.RegisterType()
.AsSelf();
builder.RegisterType()
.AsSelf();
builder.RegisterType()
.As();
builder.RegisterType()
.As();
builder.RegisterType()
.As();
//我现在在做什么
builder.RegisterType()
.As();
//…并在此处注册实例?
builder.RegisterType()
.AsSelf();
返回builder.Build();
}
同样感谢您的建议、参考或正确方向的推动。谢谢!尝试从现有的容器特定引导程序派生您的引导程序,该引导程序为您完成所有prism注册,或者从将要注册的新容器特定的
PrismApplication
类派生您的应用程序在未来的prism版本中替换引导程序
试一试,它为您创建了一个最小的、有效的应用程序。我想出来了,而且它非常简单。在我的
引导程序中
之前
类型注册得很好,但每次请求实例时都注入一个新实例,因此我将发布到一个实例,将子发布到另一个实例
builder.RegisterType<EventAggregator>()
.As<IEventAggregator>();
builder.RegisterType()
.As();
之后
我的天啊
builder.RegisterType<EventAggregator>()
.As<IEventAggregator>()
.SingleInstance;
builder.RegisterType()
.As()
.单一实例;
感谢您花时间回答。我最终找到了一个autofac解决方案(请参见我的答案),但我认为这也是一个很好的方法。请小心注册您需要的所有类型,事件聚合器只是众多解决方案中的一个……而且