自定义首页添加对话服务
定义接口与实现
添加自定义添加对话框接口
添加文件: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
{
/// <summary>
/// DialoHost名称
/// </summary>
string DialogHostName {
get; set; }
/// <summary>
/// 打开过程中执行
/// </summary>
/// <param name="parameters"></param>
void OnDialogOpend(IDialogParameters parameters);
/// <summary>
/// 确定
/// </summary>
DelegateCommand SaveCommand {
get; set; }
/// <summary>
/// 取消
/// </summary>
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
{
/// <summary>
/// 显示Dialog方法
/// </summary>
/// <param name="name"></param>
/// <param name="parameters"></param>
/// <param name="dialogHostName"></param>
/// <returns></returns>
Task<IDialogResult> ShowDialog(string name, IDialogParameters parameters, string dialogHostName = "Root");
}
}
实现IDialogHostService接口
添加文件: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<IDialogResult> ShowDialog(string name, IDialogParameters parameters, string dialogHostName = "Root")
{
if (parameters == null)
parameters = new DialogParameters();
//从容器当中去除弹出窗口的实例
var content = containerExtension.Resolve<object>(name);
//验证实例的有效性
if (!(content is FrameworkElement dialogContent))
throw new NullReferenceException("A dialog's content must be a FrameworkElement");
if (dialogContent is FrameworkElement view && view.DataContext is null && ViewModelLocator.GetAutoWireViewModel(view) is null)
ViewModelLocator.SetAutoWireViewModel(view, true);
if (!(dialogContent.DataContext is IDialogHostAware viewModel))
throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");
viewModel.DialogHostName = dialogHostName;
DialogOpenedEventHandler eventHandler = (sender, eventArgs) =>
{
if (viewModel is IDialogHostAware aware)
{
aware.OnDialogOpend(parameters);
}
eventArgs.Session.UpdateContent(content);
};
return (IDialogResult)await DialogHost.Show(dialogContent, viewModel.DialogHostName, eventHandler);
}
}
}
添加对应的窗体
添加AddMemoView
添加文件Mytodo.Views.Dialogs.AddMemoView.xaml
<UserControl
x:Class="Mytodo.Views.Dialogs.AddMemoView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Mytodo.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
d:DesignHeight="450"