08Prism WPF 入门实战 - Cmd&EeventAggregator
概要
1.Command
(1)Commnad命令
命令表示应用程序任务,并且跟踪任务是否能够被执行。在Prism里Command相关的对象都被集成到Prism框架中(namespace Prism.Commands)方便开发者使用。
命令的使用分为4个步骤。
(1)VM层定义命令(带参命令则需要在命令的尖括号内指定参数类型)
(2)View层绑定命令
(3)指定命令源
(4)指定执行命令的控件
详细内容
command的定义。
private DelegateCommand _printMsg1Command;
public DelegateCommand PrintMsg1Command { get => _printMsg1Command ?? (_printMsg1Command = new DelegateCommand(PrintMsgAction)); }
实现command需要执行的内容。
private void PrintMsgAction()
{
Debug.WriteLine("hello,word.");
}
view.xaml中调用
<Button Width="150" Height="30" Content="PrintCommand" Command="{Binding PrintMsg1Command}"></Button>
(2)CompositeCommand(复合命令)的作用为执行一组Command。
命令的使用分为5个步骤。
(1)VM层定义复合命令以及命令
(2)将命令注册到复合命令中
(3)View层绑定命令
(4)指定命令源
(5)指定执行命令的控件
详细内容
定义并实现需要被组合的command。
private DelegateCommand _printMsg1Command;
private DelegateCommand _printMsg2Command;
public DelegateCommand PrintMsg1Command { get => _printMsg1Command ?? (_printMsg1Command = new DelegateCommand(PrintMsgAction1)); }
public DelegateCommand PrintMsg2Command { get => _printMsg2Command ?? (_printMsg2Command = new DelegateCommand(PrintMsgAction2)); }
private void PrintMsgAction1()
{
Debug.WriteLine("hello,word1.");
}
private void PrintMsgAction2()
{
Debug.WriteLine("hello,word2.");
}
定义组合命令
public CompositeCommand TempCompoCommand { get => _tempCompoCommand ?? (_tempCompoCommand = new CompositeCommand()); }
//初始化组合命令
public MainWindowViewModel()
{
TempCompoCommand.RegisterCommand(PrintMsg1Command);
TempCompoCommand.RegisterCommand(PrintMsg2Command);
}
view.xaml中调用
<Button Width="150" Height="30" Content="CompoCommand" Command="{Binding TempCompoCommand}"></Button>
2.EventAggregator
EventAggregator(聚合事件),在Prism框架当中是单独的一层;例如:可用于View或Module之间做消息通知(传值)。它是由PubSubEvent<T>和IEventAggregator支撑。
聚合事件的使用分为2块。
(1)发布消息
(2)订阅消息
详细内容
- Prism中聚合事件的定义。
namespace Prism.Events
{
//
// 摘要:
// Defines a class that manages publication and subscription to events.
//
// 类型参数:
// TPayload:
// The type of message that will be passed to the subscribers.
public class PubSubEvent<TPayload> : EventBase
{
//
// 摘要:
// Subscribes a delegate to an event that will be published on the Prism.Events.ThreadOption.PublisherThread.
// Prism.Events.PubSubEvent`1 will maintain a System.WeakReference to the target
// of the supplied action delegate.
//
// 参数:
// action:
// The delegate that gets executed when the event is published.
//
// 返回结果:
// A Prism.Events.SubscriptionToken that uniquely identifies the added subscription.
//
// 言论:
// The PubSubEvent collection is thread-safe.
public SubscriptionToken Subscribe(Action<TPayload> action)
{
return Subscribe(action, ThreadOption.PublisherThread);
}
//
// 摘要:
// Subscribes a delegate to an event that will be published on the Prism.Events.ThreadOption.PublisherThread
//
// 参数:
// action:
// The delegate that gets executed when the event is raised.
//
// filter:
// Filter to evaluate if the subscriber should receive the event.
//
// 返回结果:
// A Prism.Events.SubscriptionToken that uniquely identifies the added subscription.
public virtual SubscriptionToken Subscribe(Action<TPayload> action, Predicate<TPayload> filter)
{
return Subscribe(action, ThreadOption.PublisherThread, keepSubscriberReferenceAlive: false, filter);
}
//
// 摘要:
// Subscribes a delegate to an event. PubSubEvent will maintain a System.WeakReference
// to the Target of the supplied action delegate.
//
// 参数:
// action:
// The delegate that gets executed when the event is raised.
//
// threadOption:
// Specifies on which thread to receive the delegate callback.
//
// 返回结果:
// A Prism.Events.SubscriptionToken that uniquely identifies the added subscription.
//
// 言论:
// The PubSubEvent collection is thread-safe.
public SubscriptionToken Subscribe(Action<TPayload> action, ThreadOption threadOption)
{
return Subscribe(action, threadOption, keepSubscriberReferenceAlive: false);
}
//
// 摘要:
// Subscribes a delegate to an event that will be published on the Prism.Events.ThreadOption.PublisherThread.
//
// 参数:
// action:
// The delegate that gets executed when the event is published.
//
// keepSubscriberReferenceAlive:
// When true, the Prism.Events.PubSubEvent`1 keeps a reference to the subscriber
// so it does not get garbage collected.
//
// 返回结果:
// A Prism.Events.SubscriptionToken that uniquely identifies the added subscription.
//
// 言论:
// If keepSubscriberReferenceAlive is set to false, Prism.Events.PubSubEvent`1 will
// maintain a System.WeakReference to the Target of the supplied action delegate.
// If not using a WeakReference (keepSubscriberReferenceAlive is true), the user
// must explicitly call Unsubscribe for the event when disposing the subscriber
// in order to avoid memory leaks or unexpected behavior.
// The PubSubEvent collection is thread-safe.
public SubscriptionToken