WPF Practical Study Notes 16 - Data Loading

Create a new Update event and increase the list of Prism events

Create a new file Mytodo/Common/Events/UpdateLoadingEvent.cs

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>
    {

    }
}

New base class with loading form

Create a new file Mytodo/ViewModels/NavigationViewModel.cs

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

Create data loading form extension method

Create a new file Mytodo/Extensions/DialogExtension.cs

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

Name the main window

Modify the file Mytodo/Extensions/DialogExtension.cs

<Grid>
    <materialDesign:DialogHost
        x:Name="DialogHost"
            DialogTheme="Inherit"

Main window subscription news

Modify the file Mytodo/Views/MainView.xaml.cs

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

New data loading form ProgressView

Modify the file Mytodo/Views/ProgressView.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>

Modify the viewmodel to load the form with data

Modify the file Mytodo/Views/TodoViewModel.cs

change into:

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

    }
}

Guess you like

Origin blog.csdn.net/xinzhiya001/article/details/131909456