Facebook
From jm, 4 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 244
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17.  
  18. namespace App_Project
  19. {
  20.     /// <summary>
  21.     /// Logika interakcji dla klasy MainWindow.xaml
  22.     /// </summary>
  23.     public partial class MainWindow : Window
  24.     {
  25.         private IDictionary<string, UserControl> views = new ConcurrentDictionary<string, UserControl>();
  26.         public MainWindow()
  27.         {
  28.             InitializeComponent();
  29.             MinimizeButton.Click += (s, e) => WindowState = WindowState.Minimized;
  30.             MaximizeButton.Click += (s, e) => WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
  31.             CloseButton.Click += (s, e) => Close();
  32.             StartPage();
  33.         }
  34.  
  35.         private void StartPage()
  36.         {
  37.             Home home = new Home();
  38.  
  39.             MainGrid.Children.Add(home);
  40.         }
  41.  
  42.         private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  43.         {
  44.             int index = ListViewMenu.SelectedIndex;
  45.             var item = ListViewMenu.Items[index] as ListViewItem;
  46.             string viewName = "Home";
  47.             if (item.Content != null)
  48.             {
  49.                 viewName = (item.Content as TextBlock).Text;
  50.             }
  51.             try
  52.             {
  53.                 DisplayView(viewName);
  54.             }
  55.             catch (ArgumentException ex)
  56.             {
  57.                 //not recognized viewName
  58.             }
  59.         }
  60.  
  61.         private void DisplayView(string viewName)
  62.         {
  63.             if (MainGrid == null)
  64.             {
  65.                 return;
  66.             }
  67.             MainGrid.Children.Clear();
  68.             MainGrid.Children.Add(PickCorrectView(viewName));
  69.         }
  70.  
  71.         private UserControl PickCorrectView(string viewName)
  72.         {
  73.             if (views.ContainsKey(viewName))
  74.             {
  75.                 return views[viewName];
  76.             }
  77.             else
  78.             {
  79.                 return AddNewView(viewName);
  80.             }
  81.         }
  82.  
  83.         private UserControl AddNewView(string viewName)
  84.         {
  85.             UserControl view;
  86.             switch (viewName)
  87.             {
  88.                 case "Home":
  89.                     view = new Home();
  90.                     break;
  91.                 case "Industry":
  92.                     view = new Industry();
  93.                     break;
  94.                 default:
  95.                     throw new ArgumentException("can't recognize specified view name", "viewName");
  96.             }
  97.             views.Add(viewName, view);
  98.             return view;
  99.         }
  100.     }
  101. }
  102.