public void UnregistryWindowType <ViewModel>() { Type ViewModelType = typeof(ViewModel); if (ViewModelType.IsInterface) { throw new Exception("Cannot registry interfaces"); } if (ViewModelWindowsMapping.ContainsKey(ViewModelType)) { throw new Exception($"Type is already registred"); } ViewModelWindowsMapping.Remove(ViewModelType); }
public void RegistryWindowType <ViewModel, Win>() where ViewModel : class where Win : Window, new() { Type ViewModelType = typeof(ViewModel); if (ViewModelType.IsInterface) { throw new Exception("Cannot registry interfaces"); } if (ViewModelWindowsMapping.ContainsKey(ViewModelType)) { throw new Exception($"Type is already registred"); } ViewModelWindowsMapping[ViewModelType] = typeof(Win); }
public Window CreateWindowInstanceWithViewModel(object viewModel) { if (viewModel == null) { throw new Exception("View model is null"); } Type WindowType = null; Type ViewModelType = viewModel.GetType(); while (ViewModelType != null && !ViewModelWindowsMapping.TryGetValue(ViewModelType, out WindowType)) { ViewModelType = ViewModelType.BaseType; } if (WindowType == null) { throw new Exception($"No registred window type for argument type {viewModel.GetType().FullName}"); } Window Window = (Activator.CreateInstance(WindowType) as Window); Window.DataContext = viewModel; return(Window); }