Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a smallish project that contains a singleton class called <code>ViewFinder</code> that has a couple static methods called <code>MakeWindowFor(vm)</code> and <code>MakeDialogFor(vm)</code>, that both take the viewmodel as a parameter. <code>ViewFinder</code> has a Dictionary that I fill that links viewmodels with the windows I've set to correspond to them. More information could be added, because perhaps the view lives inside another instead of simply being a window.</p> <p>This may not be the best way to accomplish the task, but works for my needs on this project, and keeps the viewmodels unaware of the actual view implementation. The ancestor of all my viewmodels contains events for things like displaying message boxes, and all my windows are descended from a base class that know how to subscribe and react to these common events.</p> <pre><code>public class ViewFinder { private static ViewFinder m_Instance; public static ViewFinder Instance { get { if (m_Instance == null) m_Instance = new ViewFinder(); return (m_Instance); } } /// Maps viewmodels to windows/dialogs. The key is the type of the viewmodel, the value is the type of the window. private Dictionary&lt;Type, Type&gt; ViewDictionary = new Dictionary&lt;Type, Type&gt;(); /// Private constructor because this is a singleton class. /// /// Registers the viewmodels/views. private ViewFinder() { Register(typeof(SomeViewModel), typeof(SomeWindowsForViewModel)); Register(typeof(SomeViewModel2), typeof(SomeWindowsForViewModel2)); } /// Registers a window with a viewmodel for later lookup. /// &lt;param name="viewModelType"&gt;The Type of the viewmodel. Must descend from ViewModelBase.&lt;/param&gt; /// &lt;param name="windowType"&gt;The Type of the view. Must descend from WindowBase.&lt;/param&gt; public void Register(Type viewModelType, Type windowType) { if (viewModelType == null) throw new ArgumentNullException("viewModelType"); if (windowType == null) throw new ArgumentNullException("windowType"); if (!viewModelType.IsSubclassOf(typeof(ViewModelBase))) throw new ArgumentException("viewModelType must derive from ViewModelBase."); if (!windowType.IsSubclassOf(typeof(WindowBase))) throw new ArgumentException("windowType must derive from WindowBase."); ViewDictionary.Add(viewModelType, windowType); } /// Finds the window registered for the viewmodel and shows it in a non-modal way. public void MakeWindowFor(ViewModelBase viewModel) { Window win = CreateWindow(viewModel); win.Show(); } /// Finds a window for a viewmodel and shows it with ShowDialog(). public bool? MakeDialogFor(ViewModelBase viewModel) { Window win = CreateWindow(viewModel); return (win.ShowDialog()); } /// Helper function that searches through the ViewDictionary and finds a window. The window is not shown here, /// because it might be a regular non-modal window or a dialog. private Window CreateWindow(ViewModelBase viewModel) { Type viewType = ViewDictionary[viewModel.GetType()] as Type; if (viewType == null) throw new Exception(String.Format("ViewFinder can't find a view for type '{0}'.", viewModel.GetType().Name)); Window win = Activator.CreateInstance(viewType) as Window; if (win == null) throw new Exception(String.Format("Activator returned null while trying to create instance of '{0}'.", viewType.Name)); win.DataContext = viewModel; return win; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload