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

新建Update事件,增加Prism事件列表

新建文件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
    {

    }
}

新建含加载窗体基类

新建文件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;

        /// 
        /// 事件聚合器
        /// 
        public readonly IEventAggregator aggregator;

        public NavigationViewModel(IContainerProvider container)
        {
            this.container = container;
            aggregator = container.Resolve();
        }
        /// 
        /// 是否允许导航
        /// 
        /// 
        /// 
        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

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
    {
        /// 
        /// 推送消息
        /// 
        /// 
        /// 
        public static void UpdateLoading(this IEventAggregator aggregator, UpdateModel model)
        {
            aggregator.GetEvent().Publish(model);
        }

        /// 
        /// 注册等待的消息
        /// 
        /// 
        /// 
        public static void Register(this IEventAggregator aggregator, Action action)
        {
            aggregator.GetEvent().Subscribe(action);
        }
    }
}

主窗口命名

修改文件Mytodo/Extensions/DialogExtension.cs


    

主窗口订阅消息

修改文件Mytodo/Views/MainView.xaml.cs

namespace Mytodo.Views
{
    /// 
    /// MainView.xaml 的交互逻辑
    /// 
    public partial class MainView : Window
    {
        /// 
        /// 订阅消息
        /// 
        public MainView(IEventAggregator aggregator)
        {
            aggregator.Register(arg =>
            {
                DialogHost.IsOpen = arg.IsOpen;

                if (DialogHost.IsOpen)
                    DialogHost.DialogContent = new ProgressView();
            });

新建数据加载窗体ProgressView

修改文件Mytodo/Views/ProgressView.xaml


    
        
    


修改viewmodel ,使用数据加载窗体

修改文件Mytodo/Views/TodoViewModel.cs

修改为:

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? todoDtos;
        private bool isRightOpen;

        public bool IsRightOpen
        {
            get { return isRightOpen; }
            set { isRightOpen = value; RaisePropertyChanged(); }
        }

        public DelegateCommand OpenRightContentCmd { set; get; }
		/// 
		/// todo集合
		/// 
		public ObservableCollection? TodoDtos
        {
			get { return todoDtos; }
			set { todoDtos = value; RaisePropertyChanged(); }
		}
        public TodoViewModel(ITodoService service,IContainerProvider provider) : base(provider)
        {
            TodoDtos=new ObservableCollection();
            OpenRightContentCmd = new DelegateCommand(Add);
            this.service = service;
        }

        /// 
        /// 获取所有数据
        /// 
        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);
        }

        /// 
        /// 打开待办事项弹窗
        /// 
        void Add()
        {
            IsRightOpen = true;
        }

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);

            GetDataAsync();
        }

    }
}

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)