Net Core教程

C#-Wpf-Prism.DryIoc-【5】事件聚合器

本文主要是介绍C#-Wpf-Prism.DryIoc-【5】事件聚合器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

【1】先完成基本的环境搭建,可参考C#-WPF-Prism.DryIoc-【1】环境搭建 - 轻吟浅唱,蓦然花开 - 博客园 (cnblogs.com)

【2】事件聚合器可用于跨ViewModel的消息传递,所以我们需要先定义一个消息实体Message.cs(根据自己的需要写)放到项目的Events文件夹下,并创建一个MessageEvent继承于泛型类PubSubEvent。代码如下

namespace Wpf_Prism.DryIoc_EventAggregator.Events
{
    public class Message
    {
        public long Id { get; set; }
        public string Content { get; set; }
    }
}
using Prism.Events;
namespace Wpf_Prism.DryIoc_EventAggregator.Events
{
    public class MessageEvent:PubSubEvent<Message>
    {
    }
}

【3】本项目结构如下图,再Views文件夹中建立了View1和View2两个用户控件和一个窗体MainView,而ViewModels下也分别建立了对应的ViewModel

 

 【4】编辑MainView.xaml 在上面放了三个按钮并绑定了命令和参数,用来切换页面和推送消息

<Window x:Class="Wpf_Prism.DryIoc_EventAggregator.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:Wpf_Prism.DryIoc_EventAggregator.Views"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">
    <DockPanel>
        <UniformGrid Columns="3" DockPanel.Dock="Top">
            <Button Content="打开View1" Command="{Binding OpenViewCommand}" CommandParameter="View1"/>
            <Button Content="打开View2" Command="{Binding OpenViewCommand}" CommandParameter="View2"/>
            <Button Content="发送消息" Command="{Binding SendMessageCommand}"/>
        </UniformGrid>
        <ContentControl prism:RegionManager.RegionName="MainContentRegion"/>
    </DockPanel>
</Window>

【5】编辑MainViewModel.cs 添加对应的命令,以及注入需要使用的IRegionManager 和 IEventAggregator接口

using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Wpf_Prism.DryIoc_EventAggregator.Events;

namespace Wpf_Prism.DryIoc_EventAggregator.ViewModels
{
    public class MainViewModel
    {
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;

        public DelegateCommand<string> OpenViewCommand { get;private set; }
        public DelegateCommand SendMessageCommand { get;private set; }
        public MainViewModel(IRegionManager regionManager,IEventAggregator eventAggregator)
        {
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
            OpenViewCommand = new DelegateCommand<string>(OpenView);
            SendMessageCommand = new DelegateCommand(SendMessage);
        }

        /// <summary>
        /// 发送消息 Hello
        /// </summary>
        private void SendMessage()=> eventAggregator.GetEvent<MessageEvent>().Publish(new Message { Content = "Hello" });

        /// <summary>
        /// 导航
        /// </summary>
        /// <param name="obj"></param>
        private void OpenView(string obj) => regionManager.Regions["MainContentRegion"].RequestNavigate(obj);
    }
}

【6】编辑View1ViewModel 和View2ViewModel代码如下

using Prism.Events;
using System.Windows;
using Wpf_Prism.DryIoc_EventAggregator.Events;

namespace Wpf_Prism.DryIoc_EventAggregator.ViewModels
{
    public class View1ViewModel
    {
        private readonly IEventAggregator eventAggregator;

        public View1ViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            //订阅
            eventAggregator.GetEvent<MessageEvent>().Subscribe(ReceivedMessage);
        }

        /// <summary>
        /// 接收到消息
        /// </summary>
        /// <param name="obj"></param>
        private void ReceivedMessage(Message obj) => MessageBox.Show("View1收到消息:" + obj.Content);
    }
}
using Prism.Events;
using System.Windows;
using Wpf_Prism.DryIoc_EventAggregator.Events;

namespace Wpf_Prism.DryIoc_EventAggregator.ViewModels
{
    public class View2ViewModel
    {
        private readonly IEventAggregator eventAggregator;

        public View2ViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            eventAggregator.GetEvent<MessageEvent>().Subscribe(ReceivedMessage);
        }

        private void ReceivedMessage(Message obj)
        {
            MessageBox.Show("View2收到消息:" + obj.Content+"\r\n后续不会收到消息");
            //取消订阅
            eventAggregator.GetEvent<MessageEvent>().Unsubscribe(ReceivedMessage);
        }
    }
}

【7】在app.xaml.cs中将View1,View1ViewModel 和View2 ,View2ViewModel注册到容器

using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Wpf_Prism.DryIoc_EventAggregator.ViewModels;
using Wpf_Prism.DryIoc_EventAggregator.Views;

namespace Wpf_Prism.DryIoc_EventAggregator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<View1, View1ViewModel>();
            containerRegistry.RegisterForNavigation<View2, View2ViewModel>();
        }
    }
}

【8】运行项目,效果如下:

完成,谢谢!

这篇关于C#-Wpf-Prism.DryIoc-【5】事件聚合器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!