可能重複:
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