0

可能重复:
WPF MVVM 新手 - ViewModel 应该如何关闭表单?

我搜索了stackoverflow,但我认为给出的答案不适用于我的,或者我无法理解如何应用它们。

我有一个沼泽标准 MVVM WPF 应用程序。MVVM 部分由一个 RelayCommand 类和一个 ViewModelBase 类以及一个扩展 ViewModelBase 的 WorkspaceViewModel 类组成。

我有两个窗口,MainWindow 和 CustomMessageBox 窗口(实际上为用户提供了一个问题和两个答案)。我在 MainWindow 中使用此代码打开 CustomMessageBox(第二个窗口):

public ICommand BrowseFileFolderCommand
    {
        get
        {
            if (_browseFileFolderCommand == null)
            {
                _browseFileFolderCommand = new RelayCommand(o =>
                    {
                        var messageViewModel = new MessageBoxViewModel("Add a Folder or File", "What do you wish to add, folder or file?", "Folder", "File");
                        var choice = new CustomMessageBox()
                        {
                            DataContext = messageViewModel
                        };
                        choice.ShowDialog();

                        if (messageViewModel.CustomMessageBoxDialogResult == DialogResult.Yes)
                        {
                            switch (messageViewModel.ChosenEntity)
                            {
                                case SelectedAnswer.Answer1:
                                    // Get folder shizz
                                    break;
                                case SelectedAnswer.Answer2:
                                    // Get file shizz
                                    break;
                                default:
                                    break;
                            }
                        }
                    }, null);
            }
            return _browseFileFolderCommand;
        }
    }

启动 CustomMessageBox 后,我无法使用 CloseCommand 将其关闭。当我尝试调试 CustomMessageBox 的加载时,似乎所有的 ICommand 在我按下任何东西之前都被触发了?

WorkspaceViewModel 具有 CloseCommand:

 #region CloseCommand

    /// <summary>
    /// Returns the command that, when invoked, attempts
    /// to remove this workspace from the user interface.
    /// </summary>
    public ICommand CloseCommand
    {
        get
        {
            if (_closeCommand == null)
                _closeCommand = new RelayCommand(param => this.OnRequestClose());

            return _closeCommand;
        }
    }

    #endregion // CloseCommand

    #region RequestClose [event]

    /// <summary>
    /// Raised when this workspace should be removed from the UI.
    /// </summary>
    public event EventHandler RequestClose;

    void OnRequestClose()
    {
        EventHandler handler = this.RequestClose;
        if (handler != null)
            handler(this, EventArgs.Empty);
    }

    #endregion // RequestClose [event]

有没有人有任何想法?我遗漏了什么重要的东西吗?

谢谢,

4

2 回答 2

1

当我需要完成此操作时,我会从我的Command.Execute逻辑中调用以下代码行:

App.Current.Windows.Cast<Window>().Where(win => win is CustomMessageBox).FirstOrDefault().Close();

我希望这有帮助。

于 2012-11-26T13:42:47.147 回答
1

我实际上并没有将任何方法附加到事件处理程序,所以当事件处理程序被调用时,什么也没做,因为代码走到了死胡同,所以我更改了代码并将窗口的关闭方法附加到事件ViewModel 的处理程序:

messageViewModel.RequestClose += (s, e) => choice.Close();

这是完整的代码:

public ICommand BrowseFileFolderCommand
{
    get
    {
        if (_browseFileFolderCommand == null)
        {
            _browseFileFolderCommand = new RelayCommand(() =>
                {
                    var messageViewModel = new MessageBoxViewModel("Add a Folder or File", "What do you wish to add, folder or file?", "Folder", "File");
                    var choice = new CustomMessageBox()
                    {
                        DataContext = messageViewModel
                    };
                    // Added this line
                    messageViewModel.RequestClose += (s, e) => choice.Close();
                    choice.ShowDialog();

                    if (messageViewModel.CustomMessageBoxDialogResult == DialogResult.Yes)
                    {
                        switch (messageViewModel.ChosenEntity)
                        {
                            case SelectedAnswer.Answer1:
                                // Get folder shizz
                                break;
                            case SelectedAnswer.Answer2:
                                // Get file shizz
                                break;
                            default:
                                break;
                        }
                    }
                }, null);
        }
        return _browseFileFolderCommand;
    }
}

感谢大家帮助我得到答案,这只是为了特别澄清我的问题。

谢谢。

于 2012-11-26T15:04:49.113 回答