1、在View文件夹下新建文件夹Dialog,新建View:AddMemoView、AddToDoView
待办已完成
2、IndexView中修改按钮,绑定方法:ExecuteCommand
3、ViewModels新建Dialogs文件夹,新建AddMemoViewModel、AddToDoViewModel
public class AddMemoViewModel : IDialogAware{public string Title {get;set;}public event Action RequestClose;public bool CanCloseDialog(){return true;}public void OnDialogClosed(){}public void OnDialogOpened(IDialogParameters parameters){}}
public class AddToDoViewModel : IDialogAware{public string Title {get; set;}public event Action RequestClose;public bool CanCloseDialog(){return true;}public void OnDialogClosed(){}public void OnDialogOpened(IDialogParameters parameters){}}
4、修改IndexViewModel
using Prism.Commands;using Prism.Mvvm;using Prism.Services.Dialogs;using System.Collections.ObjectModel;using WPFProject.Common.Models;using WPFProjectShared.Dtos;namespace WPFProject.ViewModels{public class IndexViewModel : BindableBase{public IndexViewModel(IDialogService dialog){TaskBars = new ObservableCollection();CreateTaskBars();ToDoDtos = new ObservableCollection();MemoDtos = new ObservableCollection();ExecuteCommand = new DelegateCommand(Execute);this.dialog = dialog;}private void Execute(string obj){switch (obj){case "新增待办事项":AddToDo();break;case "新增备忘录":AddMemo();break;}}private void AddMemo(){dialog.ShowDialog("AddToDoView");}private void AddToDo(){dialog.ShowDialog("AddMemoView");}public DelegateCommand ExecuteCommand { get; private set; }#region 属性/// /// 列表/// private ObservableCollection taskBars;public ObservableCollection TaskBars{get { return taskBars; }set { taskBars = value; RaisePropertyChanged(); }}/// /// 代办事项/// private ObservableCollection toDoDtos;public ObservableCollection ToDoDtos{get { return toDoDtos; }set { toDoDtos = value; RaisePropertyChanged(); }}/// /// 备忘录/// private ObservableCollection memoDtos;private readonly IDialogService dialog;public ObservableCollection MemoDtos{get { return memoDtos; }set { memoDtos = value; RaisePropertyChanged(); }}#endregionprivate void CreateTaskBars(){TaskBars.Add(new TaskBar() { Icon = "ClockFast", Title = "汇总", Content = "27", Color = "#0CA0FF", Target = "" });TaskBars.Add(new TaskBar() { Icon = "ClockCheckOutLine", Title = "已完成", Content = "24", Color = "#1ECA3A", Target = "" });TaskBars.Add(new TaskBar() { Icon = "ChartLineVariant", Title = "完成比例", Content = "89%", Color = "#02C6DC", Target = "" });TaskBars.Add(new TaskBar() { Icon = "PlaylistStar", Title = "备忘录", Content = "13", Color = "#FFA000", Target = "" });}}}
5、在App.xmal.cs中注册弹窗
//注册弹窗containerRegistry.RegisterDialog();containerRegistry.RegisterDialog();
6、F5运行项目