我是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");
}
}
发布于 2021-07-21 15:27:15
在对话框的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">
.....
https://stackoverflow.com/questions/65784829
复制相似问题