对话框的 ViewModel 必须实现 IDialogAware 接口
Posted
技术标签:
【中文标题】对话框的 ViewModel 必须实现 IDialogAware 接口【英文标题】:A dialog's ViewModel must implement the IDialogAware interface 【发布时间】:2021-04-23 08:37:16 【问题描述】:我是 C# 编程领域的新手,当我尝试开发应用程序以在任何操作之前警告用户时遇到了这个问题。我使用了 html 中的对话框。 在我的 MainWindowsViewModel 上,有使用 wpf 中 prism mvvm 的对话服务打开对话视图的方法。但是我在调试时遇到了上面的错误。
public void DialogOpen()
var message = "This is a message that should be shown in the dialog.";
_dialogService.ShowDialog("DialogView", new DialogParameters($"message=message"), r =>
if (r.Result == ButtonResult.None)
Title = "Result is None";
else if (r.Result == ButtonResult.OK)
Title = "Result is OK";
else if (r.Result == ButtonResult.Cancel)
Title = "Result is Cancel";
else
Title = "I Don't know what you did!?";
);
在我的 app.xaml.cs 上
protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterSerilog();
containerRegistry.RegisterSingleton<ISnackbarMessageQueue, SnackbarMessageQueue>();
containerRegistry.RegisterSingleton<IAppConfigurationService, AppConfigurationService>();
containerRegistry.RegisterForNavigation<MainView>();
containerRegistry.RegisterDialog<DialogView, DialogViewModel>();
在 DialogViewModel 上
public class DialogViewModel : BindableBase, IDialogAware
private DelegateCommand<string> _closeDialogCommand;
public DelegateCommand<string> CloseDialogCommand =>
_closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));
private string _message;
public string Message
get return _message;
set SetProperty(ref _message, value);
private string _title = "Notification";
public string Title
get return _title;
set SetProperty(ref _title, value);
public event Action<IDialogResult> RequestClose;
protected virtual void CloseDialog(string parameter)
ButtonResult result = ButtonResult.None;
if (parameter?.ToLower() == "true")
result = ButtonResult.OK;
else if (parameter?.ToLower() == "false")
result = ButtonResult.Cancel;
RaiseRequestClose(new DialogResult(result));
public virtual void RaiseRequestClose(IDialogResult dialogResult)
RequestClose?.Invoke(dialogResult);
public virtual bool CanCloseDialog()
return true;
public virtual void OnDialogClosed()
public virtual void OnDialogOpened(IDialogParameters parameters)
Message = parameters.GetValue<string>("message");
【问题讨论】:
有很多方法可以在 MVVM 上下文中显示消息对话框,而无需实现特殊接口 - 我在一篇博文中介绍了一种方法 peregrinesview.uk/mvvm-message-dialogs 【参考方案1】:在对话框的 xaml 中,确保在 UserControl 的标头中将 ViewModelLocator.AutowireViewModel 设置为 true。
请参阅下面的示例标题以供参考:
<UserControl x:Class="MyProject.Views.TestDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyProject.Views"
xmlns:mvvm="http://prismlibrary.com/"
mvvm:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
.....
【讨论】:
以上是关于对话框的 ViewModel 必须实现 IDialogAware 接口的主要内容,如果未能解决你的问题,请参考以下文章
准备好 Dialog ViewModel 绑定,调用 Dialog 并在 MVVM 中从它返回数据
WPF MVVM 从 ViewModel 触发事件的正确方法