08Prism WPF 入门实战 - Cmd&EeventAggregator

概要

1.Command

(1)Commnad命令

命令表示应用程序任务,并且跟踪任务是否能够被执行。在Prism里Command相关的对象都被集成到Prism框架中(namespace Prism.Commands)方便开发者使用。

命令的使用分为4个步骤。

(1)VM层定义命令(带参命令则需要在命令的尖括号内指定参数类型)

(2)View层绑定命令

(3)指定命令源

(4)指定执行命令的控件

33bb6df989c619227331065cd8c48c0c.png

详细内容

软谋的.NET全套架构视频,大多视频包含源码,录制时间(初中级是2019~2020高级架构是2020~2021),原价6499,现仅需299元。这个活动周三推出后,受到热捧,仅一个技术群就几十人抢购!最后几天活动,目录和介绍:点击下方超链接查看

太牛了!三天时间几百人加我咨询这份.NET架构视频

需要的加微zls20210502,进技术群的加微mm1552923,备注进群

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)指定执行命令的控件

a519c62ab5f1e3d2c557d271bb3088e8.png

详细内容

定义并实现需要被组合的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)订阅消息

e73fb9a7de38ddd12807767ff90bec45.png

详细内容

  • 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 Subscribe(Action<TPayload> action, bool keepSubscriberReferenceAlive)
        {
            return Subscribe(action, ThreadOption.PublisherThread, keepSubscriberReferenceAlive);
        }

        //
        // 摘要:
        //     Subscribes a delegate to an event.
        //
        // 参数:
        //   action:
        //     The delegate that gets executed when the event is published.
        //
        //   threadOption:
        //     Specifies on which thread to receive the delegate callback.
        //
        //   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 Subscribe(Action<TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive)
        {
            return Subscribe(action, threadOption, keepSubscriberReferenceAlive, null);
        }

        //
        // 摘要:
        //     Subscribes a delegate to an event.
        //
        // 参数:
        //   action:
        //     The delegate that gets executed when the event is published.
        //
        //   threadOption:
        //     Specifies on which thread to receive the delegate callback.
        //
        //   keepSubscriberReferenceAlive:
        //     When true, the Prism.Events.PubSubEvent`1 keeps a reference to the subscriber
        //     so it does not get garbage collected.
        //
        //   filter:
        //     Filter to evaluate if the subscriber should receive the event.
        //
        // 返回结果:
        //     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 virtual SubscriptionToken Subscribe(Action<TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<TPayload> filter)
        {
            IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive);
            IDelegateReference filterReference = (filter == null) ? new DelegateReference((Predicate<TPayload>)((TPayload _003Cp0_003E) => true), keepReferenceAlive: true) : new DelegateReference(filter, keepSubscriberReferenceAlive);
            EventSubscription<TPayload> eventSubscription;
            switch (threadOption)
            {
                case ThreadOption.PublisherThread:
                    eventSubscription = new EventSubscription<TPayload>(actionReference, filterReference);
                    break;
                case ThreadOption.BackgroundThread:
                    eventSubscription = new BackgroundEventSubscription<TPayload>(actionReference, filterReference);
                    break;
                case ThreadOption.UIThread:
                    if (base.SynchronizationContext == null)
                    {
                        throw new InvalidOperationException(Resources.EventAggregatorNotConstructedOnUIThread);
                    }

                    eventSubscription = new DispatcherEventSubscription<TPayload>(actionReference, filterReference, base.SynchronizationContext);
                    break;
                default:
                    eventSubscription = new EventSubscription<TPayload>(actionReference, filterReference);
                    break;
            }

            return InternalSubscribe(eventSubscription);
        }

        //
        // 摘要:
        //     Publishes the Prism.Events.PubSubEvent`1.
        //
        // 参数:
        //   payload:
        //     Message to pass to the subscribers.
        public virtual void Publish(TPayload payload)
        {
            InternalPublish(payload);
        }

        //
        // 摘要:
        //     Removes the first subscriber matching System.Action`1 from the subscribers' list.
        //
        // 参数:
        //   subscriber:
        //     The System.Action`1 used when subscribing to the event.
        public virtual void Unsubscribe(Action<TPayload> subscriber)
        {
            lock (base.Subscriptions)
            {
                IEventSubscription eventSubscription = base.Subscriptions.Cast<EventSubscription<TPayload>>().FirstOrDefault((EventSubscription<TPayload> evt) => evt.Action == subscriber);
                if (eventSubscription != null)
                {
                    base.Subscriptions.Remove(eventSubscription);
                }
            }
        }

        //
        // 摘要:
        //     Returns true if there is a subscriber matching System.Action`1.
        //
        // 参数:
        //   subscriber:
        //     The System.Action`1 used when subscribing to the event.
        //
        // 返回结果:
        //     true if there is an System.Action`1 that matches; otherwise false.
        public virtual bool Contains(Action<TPayload> subscriber)
        {
            IEventSubscription eventSubscription;
            lock (base.Subscriptions)
            {
                eventSubscription = base.Subscriptions.Cast<EventSubscription<TPayload>>().FirstOrDefault((EventSubscription<TPayload> evt) => evt.Action == subscriber);
            }

            return eventSubscription != null;
        }
    }
}
  • 定义消息传递参数Model

public class TempEventModel
{
    public string MessageType { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }
}
  • 定义消息事件

/// <summary>
/// 消息事件
/// </summary>
public class MessagerEvent : PubSubEvent<TempEventModel> //PubSubEvent<string>
{
    
}
  • 定义聚合事件(订阅方)。

private IEventAggregator _eventAggregator;

public ContactViewModel(IEventAggregator eventAggregator)
{
    //获取框架内聚合事件的引用
    _eventAggregator = eventAggregator;
}

//初始化订阅
_eventAggregator.GetEvent<MessagerEvent>().Subscribe(OnSubscribeMessage,false);

//实现订阅回调(聚合事件被触发时需要执行的内容)
private void OnSubscribeMessage(TempEventModel eventModel)
{
    //输出接收到的消息内容
    Debug.WriteLine($" Wemail.Contact receive message : { eventModel.Name },{ eventModel.Age }.");
}
  • 定义聚合事件(发送方)。

private IEventAggregator _eventAggregator;

public MainWindowViewModel(IEventAggregator eventAggregator)
{
     _eventAggregator = eventAggregator;
}

//发布消息
_eventAggregator.GetEvent<MessagerEvent>().Publish(eventModel);

EventAggregator还有取消订阅、消息过滤功能。

取消订阅:是为了管理pub/sub特性所占用的资源,富客户端应用随着业务、时间的变化而复杂。极大的可能会导致客户端里穿插上百个消息,会导致代码非常混乱。所以在代码中慎用pub/sub这个特性,或及时取消不用的订阅因为这套机制存在强引用关系不会随着作用域的结束而结束(GC无法回收),如果任由发展会导致客户端内存上涨;解决这个问题除了及时取消订阅,还可以在订阅时指定keepSubcriberReferenceAlive参数为false。

消息过滤:也是管理pub/sub的手段之一,通过指定特定的消息类型来细化区分pub/sub消息传递。

1cd18a7a345151ab059284f606bbd99c.png

  • 定义聚合事件(订阅方)。

private IEventAggregator _eventAggregator;

public ContactViewModel(IEventAggregator eventAggregator)
{
    //获取框架内聚合事件的引用
    _eventAggregator = eventAggregator;
}

//初始化订阅
_eventAggregator.GetEvent<MessagerEvent>().Subscribe(OnSubscribeMessage,
                ThreadOption.PublisherThread,false, MessageFilter);

//实现订阅回调(聚合事件被触发时需要执行的内容)
private bool MessageFilter(TempEventModel eventModel) 
{
     if (eventModel.MessageType == MessagerType.JusterMessage) return true;

     return false;
}
  • 定义聚合事件(发送方)。

private IEventAggregator _eventAggregator;

public MainWindowViewModel(IEventAggregator eventAggregator)
{
     _eventAggregator = eventAggregator;
}

//发布消息
_eventAggregator.GetEvent<MessagerEvent>().Publish(eventModel);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF Prism是微软新一代的界面开发框架,它具有丰富的功能和灵活的架构,广泛应用于桌面应用程序和Web应用程序的开发中。朝夕停车场项目是基于WPF Prism框架的一个实战项目,它实现了停车场的智能化管理,包括车辆进入和出场的自动识别,车位的实时监测和调度,以及财务管理和数据分析等功能。 该项目的源码包含了多个模块和组件,采用了MVVM架构和依赖注入技术,使代码组织和维护非常方便。项目中的主要模块包括: 1. Shell模块:该模块是整个应用程序的容器,它提供了主窗口和导航栏等界面组件,以及对其他模块的管理和协调。 2. Home模块:该模块实现了停车场的实时监控和调度功能,包括车位的占用和空闲状态显示,车辆进出场的记录和管理,以及停车位的预定和预约等功能。 3. Financial模块:该模块实现了停车场的财务管理和数据分析功能,包括车位租赁、停车费用计算和缴纳,以及停车场运营数据的统计和分析等功能。 4. Configuration模块:该模块实现了停车场的基础配置和参数管理功能,包括车位数量、收费标准和系统设置等功能。 5. Common模块:该模块包含了一些公共的模型和工具类,用于提供系统级别的服务和支持。 通过这个实战项目的源码学习,可以深入了解WPF Prism框架的应用及其MVVM架构和依赖注入的设计思想,也可以了解如何实现一个完整的智能化停车场管理系统。同时,该项目源码可以作为一个参考,通过在此基础上进行二次开发和定制,实现更加具体化的应用需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值