WPF MVVM Stylet使用文档(中文)06-The WindowManager

在传统的 "视图优先" 的方式中,如果您想要显示一个新窗口或对话框,您需要创建一个视图的新实例,然后调用 . show().showdialog()

在 ViewModel-first 方式中,您不能直接与视图交互,所以您不能这样做。WindowManager 解决了这个问题——调用 IWindowManager.ShowWindow(someViewModel) 将获取那个 ViewModel,找到它的视图,实例化它,将它绑定到那个ViewModel,并显示它。

C# VB.NET
class SomeViewModel
{
   private IWindowManager windowManager;
   public SomeViewModel(IWindowManager windowManager)
   {
      this.windowManager = windowManager;
   }
 
   public void ShowAWindow()
   {
      var viewModel = new OtherViewModel();
      this.windowManager.ShowWindow(viewModel);
   }
 
   public void ShowADialog()
   {
      var viewModel = new OtherViewModel();
      bool? result = this.windowManager.ShowDialog(viewModel);
      // result holds the return value of Window.ShowDialog()
      if (result.GetValueOrDefault(true))
      {
         // DialogResult was set to true
      }
   }
}
Class SomeViewModel
 
    Private windowManager As IWindowManager
 
    Public Sub New(ByVal windowManager As IWindowManager)
        Me.windowManager = windowManager
    End Sub
 
    Public Sub ShowAWindow()
        Dim viewModel = New OtherViewModel()
        Me.windowManager.ShowWindow(viewModel)
    End Sub
 
    Public Sub ShowADialog()
        Dim viewModel = New OtherViewModel()
        Dim result As Boolean? = Me.windowManager.ShowDialog(viewModel)
        ' Result holds the return value of Window.ShowDialog()
        If result.GetValueOrDefault(True) Then
        ' DialogResult was set to true
        End If
    End Sub
End Class

很简单! 此外,IWindowManager 的引入(而不是直接在 ViewModel 上调用方法)使测试变得更加容易。

要从视图模型中关闭窗口或对话框,请使用 Screen.RequestClose, 像这样:

C# VB.NET
class ViewModelDisplayedAsWindow
{
   // Called by pressing the 'close' button
   public void Close()
   {
      this.RequestClose();
   }
}
 
class ViewModelDisplayedAsDialog
{
   // Called by pressing the 'OK' button
   public void CloseWithSuccess()
   {
      this.RequestClose(true);
   }
}
Class ViewModelDisplayedAsWindow
 
    ' Called by pressing the  'close' button
    Public Sub Close()
        Me.RequestClose()
    End Sub
 
  End Class
 
Class ViewModelDisplayedAsDialog
 
    ' Called by pressing the 'OK' button
    Public Sub CloseWithSuccess()
        Me.RequestClose(True)
    End Sub
End Class