WPF实战学习笔记17-TodoView 添加新增、编辑、查询功能

文章目录

    • TodoView 添加新增、编辑、查询功能
      • 修改TodoViewModel.cs
      • 修改XAML
      • 修改ToDoService

TodoView 添加新增、编辑、查询功能

修改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;
using System.Windows;

namespace Mytodo.ViewModels
{
    public class TodoViewModel: NavigationViewModel
    {
        #region 命令定义

        /// 
        /// 展开侧边栏
        /// 
        public DelegateCommand OpenRightContentCmd { set; get; }

        /// 
        /// 打开选择的项
        /// 
        public DelegateCommand SelectedCommand { get; set; }

        /// 
        /// 添加、编辑 项
        /// 
        public DelegateCommand ExecuteCommand { get; set; }

        #endregion

        #region 属性定义

        /// 
        /// 当前选中项
        /// 
        public ToDoDto? CurrDto
        {
            get { return currDto; }
            set { currDto = value; RaisePropertyChanged(); }
        }

        /// 
        /// 指示侧边栏是否展开
        /// 
        public bool IsRightOpen
        {
            get { return isRightOpen; }
            set { isRightOpen = value; RaisePropertyChanged(); }
        }

        /// 
        /// todo集合
        /// 
        public ObservableCollection? TodoDtos
        {
            get { return todoDtos; }
            set { todoDtos = value; RaisePropertyChanged(); }
        }

        /// 
        /// 右侧侧边栏标题
        /// 
        public string RightContentTitle
        {
            get { return rightContentTitle; }
            set { rightContentTitle = value;RaisePropertyChanged(); }
        }

        /// 
        /// 要搜索的字符串
        /// 
        public string SearchString
        {
            get { return search; }
            set { search = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 重要字段定义

        private readonly ITodoService service;

        #endregion

        #region 字段定义
        private ToDoDto currDto;
        private bool isRightOpen;
        private ObservableCollection? todoDtos;
        private string rightContentTitle;
        private string search;



        #endregion

        #region 命令方法

        private void ExceuteCmd(string obj)
        {
            switch (obj)
            {
                case "添加":
                    Add(); break;
                case "查询":
                    Query();break;
                case "保存":
                    Save(); break;
            }
        }

        private async void Save()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(CurrDto.Title) || string.IsNullOrWhiteSpace(CurrDto.Content))
                    return;

                UpdateLoding(true);

                if(CurrDto.Id>0) //编辑项
                {
                    var updateres = await service.UpdateAsync(CurrDto);
                    if (updateres.Status)
                    {
                        var todo = TodoDtos.FirstOrDefault(t => t.Id == CurrDto.Id);
                        if (todo != null)
                        {
                            todo.Title=CurrDto.Title; 
                            todo.Content=CurrDto.Content;
                            todo.Status=todo.Status;
                        }
                    }
                    else
                    {
                        MessageBox.Show("更新失败");

                    }

                }
                else
                {       
                    //添加项
                    var add_res =   await service.AddAsync(CurrDto);
                    //刷新
                    if (add_res.Status) //如果添加成功
                    {
                        TodoDtos.Add(add_res.Result);
                    }
                    else
                    {
                        MessageBox.Show("添加失败");

                    }
                       
                }
            }
            catch
            {

            }
            finally
            {
                IsRightOpen = false;
                //卸载数据加载窗体
                UpdateLoding(false);
            }
        }

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

        private void Query()
        {
            GetDataAsync();
        }

        /// 
        /// 获取所有数据
        /// 
        async void GetDataAsync()
        {
            //调用数据加载页面
            UpdateLoding(true);

            var todoResult = await service.GetAllAsync(new MyToDo.Share.Parameters.QueryParameter { PageIndex = 0, PageSize = 100,Search=SearchString });

            if (todoResult.Status)
            {
                todoDtos.Clear();
                foreach (var item in todoResult.Result.Items)
                    todoDtos.Add(item);
            }

            //卸载数据加载页面
            UpdateLoding(false);
        }

        /// 
        /// 弹出详细信息
        /// 
        /// 
        private async void Selected(ToDoDto obj)
        {
            var todores = await service.GetFirstOfDefaultAsync(obj.Id);
            if(todores.Status)
            {
                CurrDto = todores.Result;
                IsRightOpen = true;
                RightContentTitle = "我的待办";
            }
        }
        #endregion

        public TodoViewModel(ITodoService service,IContainerProvider provider) : base(provider)
        {
            //初始化对象
            TodoDtos = new ObservableCollection();  
            RightContentTitle = "添加血雨待办";

            //初始化命令
            SelectedCommand         = new DelegateCommand(Selected);
            OpenRightContentCmd     = new DelegateCommand(Add);
            ExecuteCommand          = new DelegateCommand(ExceuteCmd);

            this.service = service;
        }


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

            GetDataAsync();
        }

    }
}

修改XAML

添加引用

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

添加绑定


    
        
        
            
            
                
                
            
        
        
        
        

添加项目的双击事件


    
        
        
            
                
            
        
        
            
            
        
        
            
            
        
        
        
            
            
        
        
    

修改ToDoService

修改MyToDo.Api/Service/ToDoService.cs

public async Task GetAllAsync(QueryParameter parameter)
{
    try
    {
        //获取数据
        var resposity = work.GetRepository();

        //根据查询条件查询
        var todos = await resposity.GetPagedListAsync(predicate: x => string.IsNullOrWhiteSpace(parameter.Search) ? true : x.Title.Contains(parameter.Search), pageIndex: parameter.PageIndex, pageSize: parameter.PageSize, orderBy: source => source.OrderByDescending(t => t.CreateDate));
        return new ApiReponse(true, todos);
    }
    catch (Exception ex)
    {
        return new ApiReponse(ex.Message, false);
    }
} 

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