輕量級MVVM框架Stylet介紹:(6) WindowManager


在傳統的View-frist方式中,如果想要顯示了一個新的Window或Dialog,需要創建一個View的實例,並且調用.Show()或.ShowDialog()方法。

在ViewModel-first方式中,不能直接與Views交互,WindowManager解決了這個問題,只需要調用IWindowManager.ShowWindow(someViewModel)就可以調用ViewModel,發現其View,實例化並顯示。

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

很優雅也很簡單!此外,引入IWindowMmanager使得測試更容易。

要關閉Window或Dialog,使用Screen.RequestClose,如下所示:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM