试图了解 Caliburn Micro EventAggregator
Trying to understand Caliburn Micro EventAggregator
我了解 EventAggregator 在 Caliburn Micro 中的工作原理,但我不明白:
我们以此为例:
视图模型 A
private IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator
_eventAggregator.PublishOnUIThread("Hello");
}
ViewModel B
private IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator
_eventAggregator.Subscribe(this)
}
public void Handle(string message)
{
MessageBox.Show(message + " From ViewModel B")
}
ViewModel C
private IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator
_eventAggregator.Subscribe(this)
}
public void Handle(string message)
{
MessageBox.Show(message + " From ViewModel C")
}
那么我如何 select 在发布事件时使用哪个视图模型句柄?
So how can i select which viewmodel handle to use when i publish events?
多个视图模型可能会订阅同一条消息。这就是事件聚合的工作原理。发布者在不知道接收者数量的情况下发布一条消息。
如果您希望特定视图模型能够订阅事件,它应该实现 IHandle<T>
接口并调用事件聚合器上的 Subscribe
方法:
public class ViewModelA : IHandle<string>
{
private readonly IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
public void Handle(string message)
{
MessageBox.Show(message);
}
}
执行此操作的所有视图模型都会收到消息。
我设法通过消息 class:
向特定视图模型发送消息
public class Message<TTarget, TValue>
{
public TValue Value { get; set; }
public Message(TValue value)
{
Value = value;
}
}
然后添加到class
IHandle<Message<ViewModel, string>>
用法:
_eventAggregator.PublishOnUIThread(new Message<ViewModel, string>("Hello from ViewModel"));
我了解 EventAggregator 在 Caliburn Micro 中的工作原理,但我不明白:
我们以此为例:
视图模型 A
private IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator
_eventAggregator.PublishOnUIThread("Hello");
}
ViewModel B
private IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator
_eventAggregator.Subscribe(this)
}
public void Handle(string message)
{
MessageBox.Show(message + " From ViewModel B")
}
ViewModel C
private IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator
_eventAggregator.Subscribe(this)
}
public void Handle(string message)
{
MessageBox.Show(message + " From ViewModel C")
}
那么我如何 select 在发布事件时使用哪个视图模型句柄?
So how can i select which viewmodel handle to use when i publish events?
多个视图模型可能会订阅同一条消息。这就是事件聚合的工作原理。发布者在不知道接收者数量的情况下发布一条消息。
如果您希望特定视图模型能够订阅事件,它应该实现 IHandle<T>
接口并调用事件聚合器上的 Subscribe
方法:
public class ViewModelA : IHandle<string>
{
private readonly IEventAggregator _eventAggregator;
public ViewModelA(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
public void Handle(string message)
{
MessageBox.Show(message);
}
}
执行此操作的所有视图模型都会收到消息。
我设法通过消息 class:
向特定视图模型发送消息public class Message<TTarget, TValue>
{
public TValue Value { get; set; }
public Message(TValue value)
{
Value = value;
}
}
然后添加到class
IHandle<Message<ViewModel, string>>
用法:
_eventAggregator.PublishOnUIThread(new Message<ViewModel, string>("Hello from ViewModel"));