WPF实战学习笔记16-数据加载

新建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();
        }

    }
}
相关推荐
虾球xz22 分钟前
游戏引擎学习第55天
学习·游戏引擎
oneouto38 分钟前
selenium学习笔记(二)
笔记·学习·selenium
sealaugh3243 分钟前
aws(学习笔记第十九课) 使用ECS和Fargate进行容器开发
笔记·学习·aws
炭烤玛卡巴卡1 小时前
学习postman工具使用
学习·测试工具·postman
thesky1234562 小时前
活着就好20241224
学习·算法
蜗牛hb2 小时前
VMware Workstation虚拟机网络模式
开发语言·学习·php
汤姆和杰瑞在瑞士吃糯米粑粑2 小时前
【C++学习篇】AVL树
开发语言·c++·学习
虾球xz2 小时前
游戏引擎学习第58天
学习·游戏引擎
LuH11242 小时前
【论文阅读笔记】Scalable, Detailed and Mask-Free Universal Photometric Stereo
论文阅读·笔记
奶香臭豆腐3 小时前
C++ —— 模板类具体化
开发语言·c++·学习