添加文件:Mytodo.Dialog.IDialogHostAware.cs
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mytodo.Dialog
{
public interface IDialogHostAware
{
///
/// DialoHost名称
///
string DialogHostName { get; set; }
///
/// 打开过程中执行
///
///
void OnDialogOpend(IDialogParameters parameters);
///
/// 确定
///
DelegateCommand SaveCommand { get; set; }
///
/// 取消
///
DelegateCommand CancelCommand { get; set; }
}
}
注意dialogHostName应与view中dialoghost 的Identifier属性一致
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mytodo.Dialog
{
public interface IDialogHostService:IDialogService
{
///
/// 显示Dialog方法
///
///
///
///
///
Task ShowDialog(string name, IDialogParameters parameters, string dialogHostName = "Root");
}
}
添加文件:Mytodo.Dialog.DialogHostService.cs
DialogHostService实现了自定义的IDialogHostService接口,以及DialogService类.
DialogService:可参考https://www.cnblogs.com/chonglu/p/15159387.html
Prism提供了一组对话服务, 封装了常用的对话框组件的功能, 例如:
- RegisterDialog/IDialogService (注册对话及使用对话)
- 打开对话框传递参数/关闭对话框返回参数
- 回调通知对话结果
using MaterialDesignThemes.Wpf;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Mytodo.Dialog
{
public class DialogHostService:DialogService, IDialogHostService
{
private readonly IContainerExtension containerExtension;
public DialogHostService(IContainerExtension containerExtension) : base(containerExtension)
{
this.containerExtension = containerExtension;
}
public async Task ShowDialog(string name, IDialogParameters parameters, string dialogHostName = "Root")
{
if (parameters == null)
parameters = new DialogParameters();
//从容器当中去除弹出窗口的实例
var content = containerExtension.Resolve
添加文件Mytodo.Views.Dialogs.AddMemoView.xaml
添加文件:Mytodo.Views.Dialogs.AddMemoViewmodel.cs
using MaterialDesignThemes.Wpf;
using Mytodo.Dialog;
using MyToDo.Share.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mytodo.ViewModels.Dialogs
{
internal class AddMemoViewModel : BindableBase, IDialogHostAware
{
public AddMemoViewModel()
{
SaveCommand = new DelegateCommand(Save);
CancelCommand = new DelegateCommand(Cancel);
}
private MemoDto model;
public MemoDto Model
{
get { return model; }
set { model = value; RaisePropertyChanged(); }
}
private void Cancel()
{
if (DialogHost.IsDialogOpen(DialogHostName))
DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
}
private void Save()
{
if (string.IsNullOrWhiteSpace(Model.Title) ||
string.IsNullOrWhiteSpace(model.Content)) return;
if (DialogHost.IsDialogOpen(DialogHostName))
{
//确定时,把编辑的实体返回并且返回OK
DialogParameters param = new DialogParameters();
param.Add("Value", Model);
DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
}
}
public string DialogHostName { get; set; }
public DelegateCommand SaveCommand { get; set; }
public DelegateCommand CancelCommand { get; set; }
public void OnDialogOpend(IDialogParameters parameters)
{
if (parameters.ContainsKey("Value"))
{
Model = parameters.GetValue("Value");
}
else
Model = new MemoDto();
}
}
}
添加文件Mytodo.Views.Dialogs.AddtodoView.xaml
添加文件:Mytodo.Views.Dialogs.AddTodoViewmodel.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; }
///
/// 删除项
///
public DelegateCommand DeleteCommand { get; set; }
#endregion
#region 属性定义
///
/// 项目状态
///
public int SelectIndex
{
get { return selectIndex; }
set { selectIndex = value; RaisePropertyChanged(); }
}
///
/// 当前选中项
///
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 int selectIndex;
private ToDoDto currDto;
private bool isRightOpen;
private ObservableCollection? todoDtos;
private string rightContentTitle;
private string search;
#endregion
#region 命令方法
///
/// 删除指定项
///
///
async private void DeleteItem(ToDoDto dto)
{
var delres = await service.DeleteAsync(dto.Id);
if (delres.Status)
{
var model = TodoDtos.FirstOrDefault(t => t.Id.Equals(dto.Id));
TodoDtos.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) //如果添加成功
{
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 UpdateDataAsync()
{
int? Status = SelectIndex == 0 ? null : SelectIndex == 2 ? 1 : 0;
var todoResult = await service.GetAllFilterAsync(new MyToDo.Share.Parameters.TodoParameter { PageIndex = 0, PageSize = 100, Search = SearchString, Status = Status });
if (todoResult.Status)
{
todoDtos.Clear();
foreach (var item in todoResult.Result.Items)
todoDtos.Add(item);
}
}
///
/// 获取所有数据
///
async void GetDataAsync()
{
//调用数据加载页面
UpdateLoding(true);
//更新数据
UpdateDataAsync();
//卸载数据加载页面
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);
DeleteCommand = new DelegateCommand(DeleteItem);
this.service = service;
}
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
GetDataAsync();
}
}
}
修改文件:"Mytodo.Views.IndexView.xaml
Background="{Binding Color}"
CornerRadius="5"
Opacity="0.9">
修改文件:Mytodo.App.xmal.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//注册服务
containerRegistry.GetContainer().Register(made: Parameters.Of.Type(serviceKey: "webUrl"));
containerRegistry.GetContainer().RegisterInstance(@"Http://localhost:19007/", serviceKey: "webUrl");
containerRegistry.Register();
containerRegistry.Register();
containerRegistry.Register();
//注册对话框
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation();
}