public static bool?ShowDialog(object viewModel) { var mapping = Mappings. Where(m => m.Value == viewModel.GetType()).FirstOrDefault(); if (mapping.Key == null) { return(false); } if (Activator.CreateInstance(mapping.Key) is Window) { VVMBinder <Window> binder = new VVMBinder <Window>(mapping.Key, viewModel); foreach (var bt in binder.ShowDialogButtons) { bt.Click += (sender, e) => { ShowWindowDialog_Click(sender, e); } } ; foreach (var mn in binder.ShowDialogMenuItems) { mn.Click += (sender, e) => { ShowWindowDialog_Click(sender, e); } } ; OpenDialogMappings.Add(binder.View as IDialog, viewModel.GetType()); return((binder.View as IDialog).ShowDialog()); } else { return(false); } void ShowWindowDialog_Click(object sender, RoutedEventArgs e) { string windowName = (sender as Button).Name.Replace("Open", "").Replace("Show", ""); Type type = Mappings.Keys.Where(k => k.Name == windowName).FirstOrDefault(); if (type != null) { ShowDialog(type); } } }
public static bool?ShowDialog(Type viewModelType) { //get a mapping where there is a desired viewModel var mapping = Mappings. Where(m => m.Value == viewModelType).FirstOrDefault(); //checks if the type is a window //bind with a window binder if so if (Activator.CreateInstance(mapping.Key) is Window) { VVMBinder <Window> binder = new VVMBinder <Window>(mapping.Key, mapping.Value); //gets all the buttons with content or name starting with show or open. //making event subscribe to the click event, that will open the window with name specified after show or open. foreach (var bt in binder.ShowDialogButtons) { bt.Click += (sender, e) => { ShowWindowDialog(sender, e); } } ; //same as of the buttons but here for the menuitems foreach (var mn in binder.ShowDialogMenuItems) { mn.Click += (sender, e) => { ShowWindowDialog(sender, e); } } ; //add the mapping to the dictionary of open mappings OpenDialogMappings.Add(binder.View as IDialog, viewModelType); //show the dialog return((binder.View as IDialog).ShowDialog()); } else { return(false); } //event handler void ShowWindowDialog(object sender, RoutedEventArgs e) { string windowName = null; if (sender is Button but) { windowName = but.Name; } else if (sender is MenuItem mni) { windowName = mni.Name; } windowName = windowName.Replace("Open", "").Replace("Show", "").Replace("Window", "").Replace("Dialog", ""); Type type = Mappings.Where(m => m.Key.Name.StartsWith(windowName)).FirstOrDefault().Value; if (type != null) { ShowDialog(type); } } }
public static void Navigate(Type parentViewModelType, string container, Type viewModelType) { //get a mapping where there is a desired viewModel var mapping = Mappings. Where(m => m.Value == viewModelType).FirstOrDefault(); //checks if the type is a window //bind with a window binder if so if (Activator.CreateInstance(mapping.Key) is Page) { VVMBinder <Page> binder = new VVMBinder <Page>(mapping.Key, mapping.Value); //gets all the buttons with content or name starting with show or open. //making event subscribe to the click event, that will open the window with name specified after show or open. foreach (var bt in binder.ShowDialogButtons) { bt.Click += (sender, e) => { ShowWindowDialog_Click(sender, e); } } ; //same as of the buttons but here for the menuitems foreach (var mn in binder.ShowDialogMenuItems) { mn.Click += (sender, e) => { ShowWindowDialog_Click(sender, e); } } ; //add the mapping to the dictionary of open mappings // OpenDialogMappings.Add(binder.View as IDialog, viewModelType); //Navigate //pages can be hosted only within a frame or a window element Page page = binder.View; IDialog host = GetOpenedDialog(parentViewModelType); ///TODO TODO TODO HERE try { var frame = ElementService.FindElements <Frame>(host as Window).Single(s => s.Name == container); frame.Content = page; pageHistory.Add(new KeyValuePair <string, object>(container, frame.Content)); } catch { throw new Exception($"{host.GetType().FullName} does not contain a Frame with name '{container}'"); } } else { } //event handler void ShowWindowDialog_Click(object sender, RoutedEventArgs e) { string windowName = null; if (sender is Button but) { windowName = but.Name; } else if (sender is MenuItem mni) { windowName = mni.Name; } windowName = windowName.Replace("Open", "").Replace("Show", "").Replace("Window", "").Replace("Dialog", ""); Type type = Mappings.Where(m => m.Key.Name.StartsWith(windowName)).FirstOrDefault().Value; if (type != null) { } } }