由于todoview 和 memoview的相似度很高,可复制todoview 的代码。
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 MemoViewModel : NavigationViewModel
{
#region 命令定义
///
/// 展开侧边栏
///
public DelegateCommand OpenRightContentCmd { set; get; }
///
/// 打开选择的项
///
public DelegateCommand SelectedCommand { get; set; }
///
/// 添加、编辑 项
///
public DelegateCommand ExecuteCommand { get; set; }
///
/// 删除项
///
public DelegateCommand DeleteCommand { get; set; }
#endregion
#region 属性定义
///
/// 项目状态
///
public int SelectIndex
{
get { return selectIndex; }
set { selectIndex = value; RaisePropertyChanged(); }
}
///
/// 当前选中项
///
public MemoDto? CurrDto
{
get { return currDto; }
set { currDto = value; RaisePropertyChanged(); }
}
///
/// 指示侧边栏是否展开
///
public bool IsRightOpen
{
get { return isRightOpen; }
set { isRightOpen = value; RaisePropertyChanged(); }
}
///
/// todo集合
///
public ObservableCollection? MemoDtos
{
get { return memoDtos; }
set { memoDtos = 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 IMemoService service;
#endregion
#region 字段定义
private int selectIndex;
private MemoDto currDto;
private bool isRightOpen;
private ObservableCollection? memoDtos;
private string rightContentTitle;
private string search;
#endregion
#region 命令方法
///
/// 删除指定项
///
///
async private void DeleteItem(MemoDto dto)
{
var delres = await service.DeleteAsync(dto.Id);
if (delres.Status)
{
var model = MemoDtos.FirstOrDefault(t => t.Id.Equals(dto.Id));
MemoDtos.Remove(dto);
}
}
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)
{
UpdateDataAsync();
}
else
{
MessageBox.Show("更新失败");
}
}
else
{
//添加项
var add_res = await service.AddAsync(CurrDto);
//刷新
if (add_res.Status) //如果添加成功
{
MemoDtos.Add(add_res.Result);
}
else
{
MessageBox.Show("添加失败");
}
}
}
catch
{
}
finally
{
IsRightOpen = false;
//卸载数据加载窗体
UpdateLoding(false);
}
}
///
/// 打开待办事项弹窗
///
void Add()
{
CurrDto = new MemoDto();
IsRightOpen = true;
}
private void Query()
{
GetDataAsync();
}
///
/// 根据条件更新数据
///
async void UpdateDataAsync()
{
var memoResult = await service.GetAllAsync(new MyToDo.Share.Parameters.QueryParameter { PageIndex = 0, PageSize = 100, Search = SearchString });
if (memoResult.Status)
{
MemoDtos.Clear();
foreach (var item in memoResult.Result.Items)
MemoDtos.Add(item);
}
}
///
/// 获取所有数据
///
async void GetDataAsync()
{
//调用数据加载页面
UpdateLoding(true);
//更新数据
UpdateDataAsync();
//卸载数据加载页面
UpdateLoding(false);
}
///
/// 弹出详细信息
///
///
private async void Selected(MemoDto obj)
{
var todores = await service.GetFirstOfDefaultAsync(obj.Id);
if (todores.Status)
{
CurrDto = todores.Result;
IsRightOpen = true;
RightContentTitle = "我的待办";
}
}
#endregion
public MemoViewModel(IMemoService service, IContainerProvider provider) : base(provider)
{
//初始化对象
MemoDtos = new ObservableCollection();
RightContentTitle = "添加备忘率";
//初始化命令
SelectedCommand = new DelegateCommand(Selected);
OpenRightContentCmd = new DelegateCommand(Add);
ExecuteCommand = new DelegateCommand(ExceuteCmd);
DeleteCommand = new DelegateCommand(DeleteItem);
this.service = service;
}
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
GetDataAsync();
}
}
}
public async Task Delete(int todoid) => await service.DeleteApublic async Task Delete(int todoid) =>