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

    }
}
相关推荐
Nu11PointerException4 分钟前
JAVA笔记 | ResponseBodyEmitter等异步流式接口快速学习
笔记·学习
亦枫Leonlew1 小时前
三维测量与建模笔记 - 3.3 张正友标定法
笔记·相机标定·三维重建·张正友标定法
考试宝2 小时前
国家宠物美容师职业技能等级评价(高级)理论考试题
经验分享·笔记·职场和发展·学习方法·业界资讯·宠物
黑叶白树3 小时前
简单的签到程序 python笔记
笔记·python
@小博的博客3 小时前
C++初阶学习第十弹——深入讲解vector的迭代器失效
数据结构·c++·学习
幸运超级加倍~4 小时前
软件设计师-上午题-15 计算机网络(5分)
笔记·计算机网络
南宫生4 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步5 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
love_and_hope5 小时前
Pytorch学习--神经网络--搭建小实战(手撕CIFAR 10 model structure)和 Sequential 的使用
人工智能·pytorch·python·深度学习·学习
Chef_Chen5 小时前
从0开始学习机器学习--Day14--如何优化神经网络的代价函数
神经网络·学习·机器学习