Need suggestion for Mutiple Windows application design
- by King Chan
This was previously posted in StackOverflow, I just moved to here...
I am using VS2008, MVVM, WPF, Prism to make a mutiple window CRM Application.
I am using MidWinow in my MainWindow, I want
Any ViewModel would able to make request to MainWindow to create/add/close MidChildWindow, ChildWindow(from WPF Toolkit), Window (the Window type).
ViewModel can get the DialogResult from the ChildWindow its excutes.
MainWindow have control on all opened window types.
Here is my current approach:
I made Dictionary of each of the windows type and stores them into MainWindow class.
For 1, i.e in a CustomerInformationView, its CustomerInformationViewModel can execute EditCommand and use EventAggregator to tell MainWindow to open a new ChildWindow.
CustomerInformationViewModel:
CustomerEditView ceView = new CustomerEditView ();
CustomerEditViewModel ceViewModel = CustomerEditViewModel ();
ceView.DataContext = ceViewModel;
ChildWindow cWindow = new ChildWindow();
cWindow.Content = ceView;
MainWindow.EvntAggregator.GetEvent<NewWindowEvent>().Publish(new WindowEventArgs(ceViewModel.ViewModeGUID, cWindow ));
cWindow.Show();
Notice that all my ViewModel will generates a Guid for help identifies the ChildWindow from MainWindow's dictionary. Since I will only be using 1 View 1 ViewModel for every Window.
For 2. In CustomerInformationViewModel I can get DialogResult by OnClosing event from ChildWindow, in CustomerEditViewModel can use Guid to tell MainWindow to close the ChildWindow.
Here is little question and problems:
Is it good idea to use Guid here? Or should I use HashKey from ChildWindow?
My MainWindows contains windows reference collections. So whenever window close, it will get notifies to remove from the collection by OnClosing event. But all the Windows itself doesn't know about its associated Guid, so when I remove it, I have to search for every KeyValuePair to compares...
I still kind of feel wrong associate ViewModel's Guid for ChildWindow, it would make more sense if ChildWindow has it own ID then ViewModel associate with it...
But most important, is there any better approach on this design? How can I improve this better?