2012-11-26 119 views
0

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

有没有人有什么想法?我是否遗漏了任何关键的东西?

感谢,

+0

你想要什么?应该终止该应用程序吗? – Tilak

+0

没有只有主应用引发的对话框 –

+0

您的意思是,类似于this.Close(); – Tilak

回答

1

我实际上并没有将任何方法附加到事件处理程序,所以当调用事件处理程序时,没有任何事情做,因为代码到了死胡同,所以我更改了代码,并附上了Close方法窗口视图模型的事件处理程序:

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; 
    } 
} 

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

谢谢。

1

当我需要做到这一点,我叫下面的代码行从我Command.Execute逻辑:

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

我希望这有助于。

+0

因此,用你的线替换我的CloseCommand逻辑? –

+0

嗯,我不知道当你的命令被触发时是否还有其他逻辑要执行,所以不管你是完全替换你现有的逻辑还是简单地在那里包含那行代码都取决于你。但是,该代码行将从应用程序中的任何位置(即使是ViewModel)关闭窗口,因此您不必在UI端(视图)上提升/处理事件以关闭所需的窗口。 – XamlZealot

+0

优秀我会稍后尝试! –