WPF Practical Study Notes 23 - Adding Functions to the Home Page

Home page add function

  • Implement the ITodoService and IMemoService interfaces, and initialize them in the constructor.
  • Create new ObservableCollection<ToDoDto>, ObservableCollection<MemoDto>typed properties and bind them to the UI
  • Modify the Addtodo and Addmemo functions to add the added function

add add function

Modify the file: 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<string> ExecuteCommand { get; set; }
        #endregion

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

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

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

        /// <summary>
        /// 首页任务条
        /// </summary>
        public ObservableCollection<TaskBar> 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<TaskBar> taskBars;
        private ObservableCollection<ToDoDto> todoDtos;
        private ObservableCollection<MemoDto> memoDtos;
        #endregion

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

        /// <summary>
        /// 添加待办事项
        /// </summary>
        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<ToDoDto>("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);
            }
            }

        }

        /// <summary>
        /// 添加备忘录
        /// </summary>
        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<MemoDto>("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<TaskBar>();
            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<ITodoService>();
            this.memoService = provider.Resolve<IMemoService>();

            //实例化对象
            MemoDtos = new ObservableCollection<MemoDto>();
            TodoDtos = new ObservableCollection<ToDoDto>();


            ExecuteCommand = new DelegateCommand<string>(Execute);

            this.dialog = dialog;

            CreatBars();
        }
    }
}

Guess you like

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