新建Update事件,增加Prism事件列表
新建文件Mytodo/Common/Events/UpdateLoadingEvent.cs
c#
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mytodo.Common.Events
{
public class UpdateModel
{
public bool IsOpen { get; set; }
}
public class UpdateLoadingEvent : PubSubEvent<UpdateModel>
{
}
}
新建含加载窗体基类
新建文件Mytodo/ViewModels/NavigationViewModel.cs
c#
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;
using Mytodo.Common.Events;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using Prism.Mvvm;
using Mytodo.Extensions;
namespace Mytodo.ViewModels
{
public class NavigationViewModel : BindableBase,INavigationAware
{
private readonly IContainerProvider container;
/// <summary>
/// 事件聚合器
/// </summary>
public readonly IEventAggregator aggregator;
public NavigationViewModel(IContainerProvider container)
{
this.container = container;
aggregator = container.Resolve<IEventAggregator>();
}
/// <summary>
/// 是否允许导航
/// </summary>
/// <param name="navigationContext"></param>
/// <returns></returns>
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public virtual void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public virtual void OnNavigatedTo(NavigationContext navigationContext)
{
}
public void UpdateLoding(bool isopen)
{
aggregator.UpdateLoading(new Common.Events.UpdateModel()
{
IsOpen = isopen
});
}
}
}
建立数据加载窗体扩展方法
新建文件Mytodo/Extensions/DialogExtension.cs
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Prism.Events;
using Mytodo.Common.Events;
namespace Mytodo.Extensions
{
public static class DialogExtension
{
/// <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="action"></param>
public static void Register(this IEventAggregator aggregator, Action<UpdateModel> action)
{
aggregator.GetEvent<UpdateLoadingEvent>().Subscribe(action);
}
}
}
主窗口命名
修改文件Mytodo/Extensions/DialogExtension.cs
c#
<Grid>
<materialDesign:DialogHost
x:Name="DialogHost"
DialogTheme="Inherit"
主窗口订阅消息
修改文件Mytodo/Views/MainView.xaml.cs
c#
namespace Mytodo.Views
{
/// <summary>
/// MainView.xaml 的交互逻辑
/// </summary>
public partial class MainView : Window
{
/// <summary>
/// 订阅消息
/// </summary>
public MainView(IEventAggregator aggregator)
{
aggregator.Register(arg =>
{
DialogHost.IsOpen = arg.IsOpen;
if (DialogHost.IsOpen)
DialogHost.DialogContent = new ProgressView();
});
新建数据加载窗体ProgressView
修改文件Mytodo/Views/ProgressView.xaml
xaml
<UserControl
x:Class="Mytodo.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:Mytodo.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<StackPanel Background="Transparent">
<ProgressBar
Width="50"
Height="50"
Background="Transparent"
IsIndeterminate="True"
Style="{StaticResource MaterialDesignCircularProgressBar}" />
</StackPanel>
</UserControl>
修改viewmodel ,使用数据加载窗体
修改文件Mytodo/Views/TodoViewModel.cs
修改为:
c#
using Mytodo.Common.Models;
using Mytodo.Service;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using MyToDo.Share.Models;
using System.Threading.Tasks;
using Prism.Regions;
namespace Mytodo.ViewModels
{
public class TodoViewModel: NavigationViewModel
{
private readonly ITodoService service;
private ObservableCollection<ToDoDto>? todoDtos;
private bool isRightOpen;
public bool IsRightOpen
{
get { return isRightOpen; }
set { isRightOpen = value; RaisePropertyChanged(); }
}
public DelegateCommand OpenRightContentCmd { set; get; }
/// <summary>
/// todo集合
/// </summary>
public ObservableCollection<ToDoDto>? TodoDtos
{
get { return todoDtos; }
set { todoDtos = value; RaisePropertyChanged(); }
}
public TodoViewModel(ITodoService service,IContainerProvider provider) : base(provider)
{
TodoDtos=new ObservableCollection<ToDoDto>();
OpenRightContentCmd = new DelegateCommand(Add);
this.service = service;
}
/// <summary>
/// 获取所有数据
/// </summary>
async void GetDataAsync()
{
//调用数据加载页面
UpdateLoding(true);
var todoResult = await service.GetAllAsync(new MyToDo.Share.Parameters.QueryParameter { PageIndex = 0, PageSize = 100 });
if (todoResult.Status)
{
todoDtos.Clear();
foreach(var item in todoResult.Result.Items)
todoDtos.Add(item);
}
//卸载数据加载页面
UpdateLoding(false);
}
/// <summary>
/// 打开待办事项弹窗
/// </summary>
void Add()
{
IsRightOpen = true;
}
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
GetDataAsync();
}
}
}