我搜索了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]
有没有人有任何想法?我遗漏了什么重要的东西吗?
谢谢,