当前位置: 首页 > article >正文

WPF实战项目十七(客户端):数据等待加载弹框动画

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:DialogHost
        x:Name="DialogHost"
        DialogTheme="Inherit"
        Identifier="RootDialog"
        SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}">

5、在View文件夹下新建用户控件ProgressView.xmal

<UserControl
    x: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">
            <ProgressBar
                Width="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();
        }
    }


http://www.kler.cn/a/146928.html

相关文章:

  • .NET Core NPOI 导出图片到Excel指定单元格并自适应宽度
  • 图像处理中实现 C++ 和 Python 的高效通信——ZeroMQ protobuf
  • 【微服务】面试 2、服务雪崩
  • React中createRoot函数原理解读——Element对象与Fiber对象、FiberRootNode与HostRootNode
  • MySQL数据导出导入
  • C语言的小项目-简易计算器
  • 「Linux」git的安装与使用
  • Android 12 打开网络ADB并禁用USB连接ADB
  • Ubuntu新手使用教程
  • 汇编:关于栈的知识
  • mybatis配置文件中配置类型别名的方式
  • 鸿蒙应用开发-初见:ArkUI
  • uni-app+vue3 封装全局函数(详细完整的方法)
  • 笔记62:注意力汇聚 --- Nadaraya_Watson 核回归
  • threejs下监听mesh事件与监听3D对象的区别
  • 28. Spring源码篇依赖注入之Optional
  • 【LeetCode】挑战100天 Day14(热题+面试经典150题)
  • Using Application Engine Meta-SQL 使用应用引擎元SQL
  • Java制作“简易王者荣耀”小游戏
  • MySQL日期函数sysdate()与now()的区别,获取当前时间,日期相关函数
  • 再探Docker:从Docker基础到跨服务器部署
  • 京东平台双11全品类完整销售数据回顾(京东大数据-京东数据采集-京东数据接口)
  • 什么是机器学习
  • 概念理论类: Linux的管道机制
  • 生成EtherCAT从站XML图片信息方法
  • VR全景技术助力政务服务大厅数字化,打造全新政务服务体验