可能重复:
WPF MVVM Newbie - how should the ViewModel close the form?开幕WPF窗口MVVM和关闭它
我已搜索周围计算器,我不认为给出的答案适用于矿井或我可以”了解如何应用它们。
我有一个沼泽标准的MVVM WPF应用程序。 MVVM部分由一个RelayCommand类和一个ViewModelBase类以及一个扩展ViewModelBase的WorkspaceViewModel类组成。
我有两个窗口,MainWindow和CustomMessageBox窗口(实际上向用户提供一个问题和两个答案)。我用这个代码在主窗口打开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的加载时,似乎所有的ICommands在我按下任何东西之前被触发了?
的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]
有没有人有什么想法?我是否遗漏了任何关键的东西?
感谢,
你想要什么?应该终止该应用程序吗? – Tilak
没有只有主应用引发的对话框 –
您的意思是,类似于this.Close(); – Tilak