WPF实战学习笔记23-首页添加功能

首页添加功能

  • 实现ITodoService、IMemoService接口,并在构造函数中初始化。
  • 新建ObservableCollectionObservableCollection类型的属性,并将其绑定到UI中
  • 修改Addtodo、Addmemo函数,将添加功能添加

添加添加函数

修改文件:Mytodo.ViewModels.IndexViewModel.cs

using Mytodo.Common.Models;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using MyToDo.Share.Models;
using Prism.Commands;
using Prism.Services.Dialogs;
using Mytodo.Dialog;
using Mytodo.ViewModels;
using Mytodo.Service;
using Prism.Ioc;
using System.Diagnostics;
using Microsoft.VisualBasic;
using ImTools;
using DryIoc;
using MyToDo.Share;

namespace Mytodo.ViewModels
{
    public class IndexViewModel:NavigationViewModel
    {
        #region 定义命令
        public DelegateCommand ExecuteCommand { get; set; }
        #endregion

        #region 定义属性
        public string Title { get; set; }

        public ObservableCollection MemoDtos
        {
            get { return memoDtos; }
            set { memoDtos = value; RaisePropertyChanged(); }
        }

        public ObservableCollection TodoDtos
        {
            get { return todoDtos; }
            set { todoDtos = value; RaisePropertyChanged(); }
        }

        /// 
        /// 首页任务条
        /// 
        public ObservableCollection TaskBars
        {
            get { return taskBars; }
            set { taskBars = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 定义重要命令

        #endregion

        #region 定义重要字段
        private readonly IDialogHostService dialog;
        private readonly ITodoService toDoService;
        private readonly IMemoService memoService;
        #endregion

        #region 定义普通字段
        private ObservableCollection taskBars;
        private ObservableCollection todoDtos;
        private ObservableCollection memoDtos;
        #endregion

        #region 命令相关方法
        /// 
        /// 选择执行命令
        /// 
        /// 
        void Execute(string obj)
        {
            switch (obj)
            {
                case "新增待办": Addtodo(null); break;
                case "新增备忘": Addmemo(null); break;
            }
        }

        /// 
        /// 添加待办事项
        /// 
        async void Addtodo(ToDoDto model)
        {
            DialogParameters param = new DialogParameters();

            if (model == null)
                param.Add("Value", model);

            var dialogres = await dialog.ShowDialog("AddTodoView", param);

                var newtodo = dialogres.Parameters.GetValue("Value");

                if (newtodo == null || string.IsNullOrEmpty(newtodo.Title) || (string.IsNullOrEmpty(newtodo.Content)))
                    return;

                if (dialogres.Result == ButtonResult.OK)
                {
                try
                {


                    if (newtodo.Id > 0)
                    {
                        var updres = await toDoService.UpdateAsync(newtodo);
                        if (updres.Status)
                        {
                            var todo = TodoDtos.FindFirst(predicate: x => x.Id == newtodo.Id);
                            //更新信息
                            todo.Content = newtodo.Content;
                            todo.Title = newtodo.Title;
                            todo.Status = newtodo.Status;
                        }
                    }
                    else
                    {
                        //添加内容 

                        //更新数据库数据
                        var addres  = await toDoService.AddAsync(newtodo);

                        //更新UI数据
                        if (addres.Status)
                        {
                            TodoDtos.Add(newtodo);
                        }
                    }
                }
           
            catch 
            {


            }
            finally
            {
                UpdateLoding(false);
            }
            }

        }

        /// 
        /// 添加备忘录
        /// 
        async void Addmemo(MemoDto model)
        {
            DialogParameters param = new DialogParameters();

            if (model == null)
                param.Add("Value", model);

            var dialogres = await dialog.ShowDialog("AddMemoView", param);

            

            if (dialogres.Result == ButtonResult.OK)
            {
                try
                {
                    var newmemo = dialogres.Parameters.GetValue("Value");

                    if (newmemo.Id > 0)
                    {
                        var updres = await memoService.UpdateAsync(newmemo);
                        if (updres.Status)
                        {
                            var memo = MemoDtos.FindFirst(predicate: x => x.Id == newmemo.Id);
                            //更新信息
                            memo.Content = newmemo.Content;
                            memo.Title = newmemo.Title;
                        }
                    }
                    else
                    {
                        //添加内容

                        var addres= await memoService.AddAsync(newmemo);

                        //更新UI数据
                        if (addres.Status)
                        {
                            MemoDtos.Add(newmemo);
                        }
                    }
                }

                catch
                {


                }
                finally
                {
                    UpdateLoding(false);
                }
            }
        }

        #endregion

        #region 其它方法

        #endregion

        #region 启动项相关
        void CreatBars()
        {
            Title = "您好,2022";
            TaskBars = new ObservableCollection();
            TaskBars.Add(new TaskBar { Icon = "CalendarBlankOutline", Title = "汇总", Color = "#FF00FF00", Content = "27", Target = "" });
            TaskBars.Add(new TaskBar { Icon = "CalendarMultipleCheck", Title = "已完成", Color = "#6B238E", Content = "24", Target = "" });
            TaskBars.Add(new TaskBar { Icon = "ChartLine", Title = "完成比例", Color = "#32CD99", Content = "100%", Target = "" });
            TaskBars.Add(new TaskBar { Icon = "CheckboxMarked", Title = "备忘录", Color = "#5959AB", Content = "13", Target = "" });
        }
        #endregion


        public IndexViewModel(IContainerProvider provider,
            IDialogHostService dialog) : base(provider)
        {
            //实例化接口
            this.toDoService= provider.Resolve();
            this.memoService = provider.Resolve();

            //实例化对象
            MemoDtos = new ObservableCollection();
            TodoDtos = new ObservableCollection();


            ExecuteCommand = new DelegateCommand(Execute);

            this.dialog = dialog;

            CreatBars();
        }
    }
}

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