我已經閱讀了WPF(以及一般的圖形用戶界面)的一個很好的實踐,說盡可能少的窗口打開。但有時候,你根本沒有選擇。如何使用WPF打開一個尊重良好實踐的新窗口?
所以我想到了一個快速的優雅的解決方案,以打開一個新的窗口,我認爲是這樣的:
public static class WinManager
{
private static Dictionary<Type, Func<Window>> collection
= new Dictionary<Type, Func<Window>>();
/* Bind the type of the ViewModel with a lambda that build an
* instance of a window*/
public static void Bind(Func<Window> ctor, Type type) { ... }
/* Search in the dictionary the specified type and show the window
* returned by the lambda*/
public static void Show(Type type){ ... }
/* Search in the dictionary the specified type and show the dialogue
* returned by the lambda*/
public static void ShowDialog(Type type) { ... }
}
type
是視圖模型的綁定到視圖(這是窗口)和拉姆達類型ctor
用於返回窗口的新實例。
管理這樣的窗口是個好主意還是我完全錯了?
關於用戶體驗或關於在WPF(以及一般GUI)中處理Windows的最佳實踐是否談論?作爲最終用戶,我寧願使用較少的窗口工作。如果它關於用戶體驗,那麼我認爲你應該解決它的要求,而不是代碼。 – Ramesh 2012-04-19 09:28:26
問題是關於從ViewModel在MVVM中打開Windows,該ViewModel應該是View不可知的。 – 2012-04-19 09:31:51
只是不使用靜態助手,在視圖模型構造函數中將這些東西作爲服務注入。這將爲您節省大量時間編寫單元測試。 – v00d00 2012-04-19 10:11:25