文章目录
- 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 命令定义/// <summary>/// 展开侧边栏/// </summary>public DelegateCommand OpenRightContentCmd { set; get; }/// <summary>/// 打开选择的项/// </summary>public DelegateCommand<ToDoDto> SelectedCommand { get; set; }/// <summary>/// 添加、编辑 项/// </summary>public DelegateCommand<string> ExecuteCommand { get; set; }#endregion#region 属性定义/// <summary>/// 当前选中项/// </summary>public ToDoDto? CurrDto{get { return currDto; }set { currDto = value; RaisePropertyChanged(); }}/// <summary>/// 指示侧边栏是否展开/// </summary>public bool IsRightOpen{get { return isRightOpen; }set { isRightOpen = value; RaisePropertyChanged(); }}/// <summary>/// todo集合/// </summary>public ObservableCollection<ToDoDto>? TodoDtos{get { return todoDtos; }set { todoDtos = value; RaisePropertyChanged(); }}/// <summary>/// 右侧侧边栏标题/// </summary>public string RightContentTitle{get { return rightContentTitle; }set { rightContentTitle = value;RaisePropertyChanged(); }}/// <summary>/// 要搜索的字符串/// </summary>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<ToDoDto>? 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);}}/// <summary>/// 打开待办事项弹窗/// </summary>void Add(){CurrDto = new ToDoDto();IsRightOpen = true;}private void Query(){GetDataAsync();}/// <summary>/// 获取所有数据/// </summary>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);}/// <summary>/// 弹出详细信息/// </summary>/// <param name="obj"></param>private async void Selected(ToDoDto obj){var todores = await service.GetFirstOfDefaultAsync(obj.Id);if(todores.Status){CurrDto = todores.Result;IsRightOpen = true;RightContentTitle = "我的待办";}}#endregionpublic TodoViewModel(ITodoService service,IContainerProvider provider) : base(provider){//初始化对象TodoDtos = new ObservableCollection<ToDoDto>(); RightContentTitle = "添加血雨待办";//初始化命令SelectedCommand = new DelegateCommand<ToDoDto>(Selected);OpenRightContentCmd = new DelegateCommand(Add);ExecuteCommand = new DelegateCommand<string>(ExceuteCmd);this.service = service;}public override void OnNavigatedTo(NavigationContext navigationContext){base.OnNavigatedTo(navigationContext);GetDataAsync();}}
}
修改XAML
添加引用
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
添加绑定
<md:DrawerHost.RightDrawerContent><DockPanelMinWidth="200"MaxWidth="240"Margin="2"LastChildFill="False"><TextBlockMargin="10"DockPanel.Dock="Top"FontFamily="微软雅黑"FontSize="20"FontWeight="Bold"Text="{Binding RightContentTitle}" /><StackPanelMargin="10"DockPanel.Dock="Top"Orientation="Horizontal"><TextBlockMargin="5"VerticalAlignment="Center"FontFamily="微软雅黑"FontSize="14"Text="状态" /><ComboBox Margin="5"><ComboBoxItem Content="已完成" FontSize="12" /><ComboBoxItem Content="未完成" FontSize="12" /></ComboBox></StackPanel><TextBoxMargin="10"md:HintAssist.Hint="待办事项标题"DockPanel.Dock="Top"FontFamily="微软雅黑"FontSize="12"Text="{Binding CurrDto.Title}" /><TextBoxMinHeight="50"Margin="10"md:HintAssist.Hint="待办事项内容"DockPanel.Dock="Top"FontFamily="微软雅黑"FontSize="12"Text="{Binding CurrDto.Content}"TextWrapping="Wrap" /><ButtonMargin="10,5"HorizontalAlignment="Center"Command="{Binding ExecuteCommand}"CommandParameter="保存"Content="保存"DockPanel.Dock="Top" /></DockPanel>
</md:DrawerHost.RightDrawerContent>
添加项目的双击事件
<Border MinWidth="200" Margin="10"><Grid MinHeight="150"><!-- 给项目添加行为 --><i:Interaction.Triggers><i:EventTrigger EventName="MouseLeftButtonUp"><i:InvokeCommandAction Command="{Binding DataContext.SelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" CommandParameter="{Binding}" /></i:EventTrigger></i:Interaction.Triggers><Grid.RowDefinitions><RowDefinition Height="auto" /><RowDefinition /></Grid.RowDefinitions><DockPanel LastChildFill="False"><TextBlockMargin="10,10"FontFamily="黑体"FontSize="14"Text="{Binding Title}" /><md:PackIconMargin="10,10"VerticalContentAlignment="Top"DockPanel.Dock="Right"Kind="More" /></DockPanel><TextBlockGrid.Row="1"Margin="10,5"FontFamily="黑体"FontSize="12"Opacity="0.7"Text="{Binding Content}" /><Canvas Grid.RowSpan="2" ClipToBounds="True"><BorderCanvas.Top="10"Canvas.Right="-50"Width="120"Height="120"Background="#FFFFFF"CornerRadius="100"Opacity="0.1" /><BorderCanvas.Top="80"Canvas.Right="-30"Width="120"Height="120"Background="#FFFFFF"CornerRadius="100"Opacity="0.1" /></Canvas><BorderGrid.RowSpan="2"Background="#ffcccc"CornerRadius="5"Opacity="0.3" /></Grid>
</Border>
修改ToDoService
修改MyToDo.Api/Service/ToDoService.cs
public async Task<ApiReponse> GetAllAsync(QueryParameter parameter)
{try{//获取数据var resposity = work.GetRepository<Todo>();//根据查询条件查询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);}
}