更新时间:2022-12-04 22:41:00
使用Prism EventAggregator.
With Prism EventAggregator.
1..创建要发布的事件
public class CloseTabEvent : PubSubEvent<TPayload>
{
}
其中TPayload
是您要传递的类型(int, string, or even class object
)
where TPayload
is type you are passing(int, string, or even class object
)
2..在您的关闭按钮所在的子视图中,发布该事件. 在您的关闭按钮Command(执行方法)中,发布该事件.
2. In your subView where your close button is, publish that event. In your close button Command (execute method) u publish that event.
private void OnCloseExecuted(object obj)
{
_eventAggregator.GetEvent<CloseTabEvent>().Publish(SomethingThatYouPublis..ThisIsTPayload);
}
在您的subView构造函数中,传递IEventAggregator
并创建一个私有字段.
In your subView constructor pass IEventAggregator
and make a private field.
private IEventAggregator _eventAggregator;
public SubViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
3..在mainViewModel中,您还传递了IEventAggregator
并在构造函数内进行订阅.
3. In your mainViewModel you also pass IEventAggregator
and subscribe inside constructor.
public MainViewModel((IEventAggregator eventAggregator)
{
eventAggregator.GetEvent<CloseTabEvent>
().Subscribe(MethodForClosingThatSpecificTab);
}
就是这样.
重要提示::使用某些IoC(Unity,Autofac ..)解析IEventAggregator
时,请使其成为单例,因此它是整个应用程序中的一个.
IMPORTANT: When resolving IEventAggregator
with some IoC(Unity, Autofac..) make it singleton, so it's one for whole app.