1、在Common文件夹下新建文件夹Events,新建扩展类UpdateLoadingEvent
public class UpdateModel {public bool IsOpen { get; set; }}internal class UpdateLoadingEvent : PubSubEvent<UpdateModel>{}
2、新建一个静态扩展类DialogExtensions来编写注册和推送等待消息弹框方法
public static class DialogExtensions{/// <summary>/// 推送等待消息/// </summary>/// <param name="aggregator"></param>/// <param name="model"></param>public static void UpdateLoading(this IEventAggregator aggregator, UpdateModel model){aggregator.GetEvent<UpdateLoadingEvent>().Publish(model);}/// <summary>/// 注册等待消息/// </summary>/// <param name="aggregator"></param>/// <param name="model"></param>public static void Register(this IEventAggregator aggregator, Action<UpdateModel> model){aggregator.GetEvent<UpdateLoadingEvent>().Subscribe(model);}}
3、在ViewModel中添加实现类NavigationViewModel
public class NavigationViewModel : BindableBase, INavigationAware{private readonly IContainerProvider containerProvider;private readonly IEventAggregator aggregator;public NavigationViewModel(IContainerProvider containerProvider){this.containerProvider = containerProvider;aggregator = containerProvider.Resolve<IEventAggregator>();}public virtual bool IsNavigationTarget(NavigationContext navigationContext){return true;}public virtual void OnNavigatedFrom(NavigationContext navigationContext){}public virtual void OnNavigatedTo(NavigationContext navigationContext){}public void UpdateLoading(bool IsOpen){aggregator.UpdateLoading(new Common.Events.UpdateModel(){IsOpen = IsOpen});}}
4、在主窗体MainView.xmal中给弹窗界面命名
<materialDesign:DialogHostx:Name="DialogHost"DialogTheme="Inherit"Identifier="RootDialog"SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}">
5、在View文件夹下新建用户控件ProgressView.xmal
<UserControlx:Class="WPFProject.Views.ProgressView"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:local="clr-namespace:WPFProject.Views"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"d:DesignHeight="450"d:DesignWidth="800"mc:Ignorable="d"><Grid><StackPanel VerticalAlignment="Center"><ProgressBarWidth="50"Height="50"Margin="20"IsIndeterminate="True"Style="{StaticResource MaterialDesignCircularProgressBar}" /></StackPanel></Grid>
</UserControl>
6、在MainView.xmal.cs中注册消息
public MainView(IEventAggregator aggregator){InitializeComponent();//注册等待消息窗口aggregator.Register(arg =>{DialogHost.IsOpen = arg.IsOpen;if (DialogHost.IsOpen)DialogHost.DialogContent = new ProgressView();});
7、修改ToDoViewModel的代码,继承NavigationViewModel
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFProject.Common.Models;
using WPFProject.Service;namespace WPFProject.ViewModels
{public class ToDoViewModel : NavigationViewModel{private readonly IToDoService toDoService;public ToDoViewModel(IToDoService toDoService, IContainerProvider provider) : base(provider){ToDoDtos = new ObservableCollection<ToDoDto>();AddCommand = new DelegateCommand(Add);this.toDoService = toDoService;}/// <summary>/// 添加待办/// </summary>/// <exception cref="NotImplementedException"></exception>private void Add(){IsIsRightDrawerOpens = true;}public DelegateCommand AddCommand { get; private set; }private bool isIsRightDrawerOpens;/// <summary>/// 右侧新增窗口是否打开/// </summary>public bool IsIsRightDrawerOpens{get { return isIsRightDrawerOpens; }set { isIsRightDrawerOpens = value; RaisePropertyChanged(); }}private ObservableCollection<ToDoDto> toDoDtos;public ObservableCollection<ToDoDto> ToDoDtos{get { return toDoDtos; }set { toDoDtos = value; }}/// <summary>/// 获取数据/// </summary>private async void GetDataAsync(){UpdateLoading(true);var todoResult = await toDoService.GetAllPageListAsync(new WPFProjectShared.Parameters.QueryParameter{PageIndex = 0,PageSize = 100});if (todoResult.Status){toDoDtos.Clear();foreach (var item in todoResult.Result.Items){toDoDtos.Add(item);}}UpdateLoading(false);}public override void OnNavigatedTo(NavigationContext navigationContext){base.OnNavigatedTo(navigationContext);GetDataAsync();}}
}
8、F5运行项目
9、同步修改MemoViewModel.cs
public class MemoViewModel : NavigationViewModel{private readonly IMemoService memoService;public MemoViewModel(IMemoService memoService, IContainerProvider provider) : base(provider){MemoDtos = new ObservableCollection<MemoDto>();AddCommand = new DelegateCommand(Add);this.memoService = memoService;}private void Add(){IsIsRightDrawerOpens = true;}public DelegateCommand AddCommand { get; private set; }private bool isIsRightDrawerOpens;public bool IsIsRightDrawerOpens{get { return isIsRightDrawerOpens; }set { isIsRightDrawerOpens = value; RaisePropertyChanged(); }}private ObservableCollection<MemoDto> memoDtos;public ObservableCollection<MemoDto> MemoDtos{get { return memoDtos; }set { memoDtos = value; RaisePropertyChanged(); }}private async void GetDataAsync(){UpdateLoading(true);var memoResult = await memoService.GetAllPageListAsync(new WPFProjectShared.Parameters.QueryParameter { PageIndex = 0, PageSize = 100 });if (memoResult.Status){memoDtos.Clear();foreach (var item in memoResult.Result.Items){memoDtos.Add(item);}}UpdateLoading(false);}public override void OnNavigatedTo(NavigationContext navigationContext){base.OnNavigatedTo(navigationContext);GetDataAsync();}}