详细代码:https://github.com/DongLiqiang/Mytodo/commit/221de6b2344d5c861f1d3b2fbb2480e3e3b35c26
修改文件Mytodo.Extensions.DialogExtension
添加内容,类中添加内容
///
/// 显示方法
///
///
///
///
///
public static async Task Question(this IDialogHostService dialogHost,string Title, string Content, string dialogHostName = "Root")
{
DialogParameters param = new DialogParameters();
//添加参数
param.Add("Title", Title);
param.Add("Content", Content);
param.Add("DialogHostName", dialogHostName);
//返回对话框实例
return await dialogHost.ShowDialog("MsgView",param, dialogHostName);
}
添加文件Mytodo.Views.MsgView.xaml
添加文件:Mytodo.ViewModels.MsgViewModel
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
{
class MsgViewModel
: BindableBase, IDialogHostAware
{
#region 命令定义
#endregion
#region 属性定义
///
/// 对话框内容
///
public string Content
{
get { return content; }
set { content = value; }
}
///
/// 对话框标题
///
public string Title
{
get { return title; }
set { title = value; }
}
#endregion
#region 重要字段定义
#endregion
#region 字段定义
private string content;
private string title;
#endregion
public MsgViewModel()
{
SaveCommand = new DelegateCommand(Save);
CancelCommand = new DelegateCommand(Cancel);
}
///
/// 取消
///
private void Cancel()
{
if (DialogHost.IsDialogOpen(DialogHostName))
DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No)); //取消返回NO告诉操作结束
}
///
/// 确定
///
private void Save()
{
if (DialogHost.IsDialogOpen(DialogHostName))
{
//确定时,把编辑的实体返回并且返回OK
DialogParameters param = new DialogParameters();
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)
{
//获取Title
if (parameters.ContainsKey("Title"))
Title = parameters.GetValue("Title");
//获取Content
if (parameters.ContainsKey("Content"))
Content = parameters.GetValue("Content");
}
}
}
修改:App.xaml.cs
RegisterTypes函数添加
containerRegistry.RegisterForNavigation();
修改文件:Mytodo.Views.MainView.cs
修改为
using Mytodo.Common.Events;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Prism.Events;
using Mytodo.Extensions;
using MaterialDesignThemes.Wpf;
using Mytodo.Dialog;
using Prism.Ioc;
namespace Mytodo.Views
{
///
/// MainView.xaml 的交互逻辑
///
public partial class MainView : Window
{
private readonly IDialogHostService dialoghost;
///
/// 订阅消息
///
public MainView(IEventAggregator aggregator,IContainerProvider provider)
{
//检索实例
dialoghost= provider.Resolve();
aggregator.Register(arg =>
{
DialogHost.IsOpen = arg.IsOpen;
if (DialogHost.IsOpen)
DialogHost.DialogContent = new ProgressView();
});
InitializeComponent();
menuBar.SelectionChanged += (s, e) =>
{
drawerHost.IsLeftDrawerOpen = false;
};
btnclo.Click += async (s, e) =>
{
//展开确认对话框
var queres = await DialogExtension.Question(dialoghost, "温馨提示", $"是否要关闭此程序?");
if (queres.Result != Prism.Services.Dialogs.ButtonResult.OK) return;
this.Close();
};
btnmin.Click += (s, e) =>
{
this.WindowState = WindowState.Minimized;
};
btnmax.Click += (s, e) =>
{
this.WindowState = WindowState.Maximized;
};
ColorZone.MouseMove += (s, e) =>
{
if (e.LeftButton == MouseButtonState.Pressed)
this.DragMove();
};
ColorZone.MouseDoubleClick += (s, e) =>
{
if (WindowState != WindowState.Normal)
WindowState = WindowState.Normal;
else
WindowState = WindowState.Maximized;
};
}
}
}
修改文件Mytodo.ViewModels.TodoViewModel.cs
添加内容
private readonly IDialogHostService dialoghost;
修改内容:
public TodoViewModel(ITodoService service,IContainerProvider provider) : base(provider)
{
//检索对话框的实例
dialoghost = provider.Resolve();
//初始化对象
TodoDtos = new ObservableCollection();
RightContentTitle = "添加血雨待办";
//初始化命令
SelectedCommand = new DelegateCommand(Selected);
OpenRightContentCmd = new DelegateCommand(Add);
ExecuteCommand = new DelegateCommand(ExceuteCmd);
DeleteCommand = new DelegateCommand(DeleteItem);
this.service = service;
}
///
/// 删除指定项
///
///
async private void DeleteItem(ToDoDto dto)
{
//展开确认对话框
var queres = await DialogExtension.Question(dialoghost, "温馨提示", $"是否要删除此项?{dto.Title}?");
if (queres.Result != Prism.Services.Dialogs.ButtonResult.OK) return;
var delres = await service.DeleteAsync(dto.Id);
if (delres.Status)
{
var model = TodoDtos.FirstOrDefault(t => t.Id.Equals(dto.Id));
TodoDtos.Remove(dto);
}
}
修改文件Mytodo.ViewModels.MemoViewModel.cs
添加内容
private readonly IDialogHostService dialoghost;
修改内容
///
/// 删除指定项
///
///
async private void DeleteItem(MemoDto dto)
{
//展开确认对话框
var queres = await DialogExtension.Question(dialoghost,"温馨提示",$"是否要删除此项?{dto.Title}?");
if (queres.Result != Prism.Services.Dialogs.ButtonResult.OK) return;
var delres = await service.DeleteAsync(dto.Id);
if (delres.Status)
{
var model = MemoDtos.FirstOrDefault(t => t.Id.Equals(dto.Id));
MemoDtos.Remove(dto);
}
}
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);
//检索DialogHostService类型的实例
dialoghost = provider.Resolve();
this.service = service;
}