首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对话框的ViewModel必须实现IDialogAware接口

对话框的ViewModel必须实现IDialogAware接口
EN

Stack Overflow用户
提问于 2021-01-19 10:46:05
回答 1查看 273关注 0票数 1

我是c#编程领域的新手,当我尝试开发应用程序以在任何操作之前警告用户时,我遇到了这个问题。我用过类似html的对话框。在我的MainWindowsViewModel上,有一个在wpf中使用prism mvvm的对话服务打开对话视图的方法。但我在调试时得到了上面的错误。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSerilog();
        containerRegistry.RegisterSingleton<ISnackbarMessageQueue, SnackbarMessageQueue>();
        containerRegistry.RegisterSingleton<IAppConfigurationService, AppConfigurationService>();
        containerRegistry.RegisterForNavigation<MainView>();
        containerRegistry.RegisterDialog<DialogView, DialogViewModel>();
        
    }

在DialogViewModel上

代码语言:javascript
运行
复制
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");
}
}
EN

回答 1

Stack Overflow用户

发布于 2021-07-21 15:27:15

在对话框的xaml中,确保将UserControl头中的ViewModelLocator.AutowireViewModel设置为true。

参考示例头部:

代码语言:javascript
运行
复制
<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">
         
         .....
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65784829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档