Microscopic Traffic Simulator
App.xaml.cs
Go to the documentation of this file.
5 using System;
6 using System.Globalization;
7 using System.IO;
8 using System.Threading;
9 using System.Windows;
10 using System.Windows.Markup;
11 
13 {
17  public partial class App : Application
18  {
22  private String localizationDirectory;
23 
27  internal event EventHandler LanguageChangedEvent;
28 
32  private int langDictId = -1;
33 
37  static App()
38  {
39  // This code is used to test the app when using other cultures.
40  //
41  //System.Threading.Thread.CurrentThread.CurrentCulture =
42  // System.Threading.Thread.CurrentThread.CurrentUICulture =
43  // new System.Globalization.CultureInfo("sk-SK");
44 
45  // Ensure the current culture passed into bindings is the OS culture.
46  // By default, WPF uses en-US as the culture, regardless of the system settings.
47  FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
48  new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(
49  CultureInfo.CurrentCulture.IetfLanguageTag)));
50  }
51 
55  public App()
56  : base()
57  {
58  //set event handler for unhandled exceptions
59  Dispatcher.UnhandledException += Dispatcher_UnhandledException;
60  localizationDirectory = System.IO.Path.GetDirectoryName(
61  System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\Localization";
62  }
63 
68  protected override void OnStartup(StartupEventArgs e)
69  {
70  base.OnStartup(e);
71  //initialize languages
72  InitializeLanguageResource();
73  //initialize main window
74  MainWindow window = new MainWindow();
75  //set main viewmodel to main window data context
76  MainViewModel mainViewModel = new MainViewModel(new GUIInteractions(this),
77  new Settings(), new Action<string>(i => { SwitchLanguage(i); }));
78  mainViewModel.Initialize(FindStringResource("new"),
79  Resources.MergedDictionaries[langDictId]["ResourceDictionaryName"].ToString().Substring(4));
80  window.DataContext = mainViewModel;
81  //show window
82  window.Show();
83  }
84 
91  internal string FindStringResource(string key)
92  {
93  return FindResource(key).ToString().Trim();
94  }
95 
100  private void InitializeLanguageResource()
101  {
102  string UICulture = Microscopic_Traffic_Simulator.Properties.Settings.Default.UICulture
103  == string.Empty ?
104  CultureInfo.CurrentUICulture.Name :
106 
107  SetLanguageResourceDictionary(GetLocXAMLFilePath(UICulture));
108  }
109 
115  private string GetLocXAMLFilePath(string inFiveCharLang)
116  {
117  string locXamlFile = "LocalizationDictionary." + inFiveCharLang + ".xaml";
118  return Path.Combine(localizationDirectory, inFiveCharLang, locXamlFile);
119  }
120 
125  private void SwitchLanguage(string inFiveCharLang)
126  {
127  //check if language is not already set as current language.
128  if (Resources.MergedDictionaries[langDictId]["ResourceDictionaryName"]
129  .ToString().Substring(4).Equals(inFiveCharLang))
130  return;
131 
132  //set new culture, save culture string to application settings and save it.
133  var ci = new CultureInfo(inFiveCharLang);
134  Thread.CurrentThread.CurrentUICulture = ci;
135  Microscopic_Traffic_Simulator.Properties.Settings.Default.UICulture = inFiveCharLang;
137 
138  //set new language resource according to new culture
139  SetLanguageResourceDictionary(GetLocXAMLFilePath(inFiveCharLang));
140  //send event signal about changing of language
141  if (LanguageChangedEvent != null)
142  {
143  LanguageChangedEvent(this, new EventArgs());
144  }
145  }
146 
152  private void SetLanguageResourceDictionary(String inFile)
153  {
154  //check if the language is supported. If not use English (or exceptionally Slovak in
155  //case that czech language is given
156  if (!File.Exists(inFile))
157  {
158  if (inFile.Contains("cs-CZ"))
159  inFile = inFile.Replace("cs-CZ", "sk-SK");
160  else
161  inFile = inFile.Replace(inFile.Substring(inFile.Length - 10, 5), "en-US");
162  }
163 
164  // Read in ResourceDictionary File
165  var languageDictionary = new ResourceDictionary();
166  languageDictionary.Source = new Uri(inFile);
167 
168  // Remove any previous Localization dictionaries loaded
169  langDictId = -1;
170  for (int i = 0; i < Resources.MergedDictionaries.Count; i++)
171  {
172  var md = Resources.MergedDictionaries[i];
173  // Make sure your Localization ResourceDictionarys have the ResourceDictionaryName
174  // key and that it is set to a value starting with "Loc-".
175  if (md.Contains("ResourceDictionaryName"))
176  {
177  if (md["ResourceDictionaryName"].ToString().StartsWith("Loc-"))
178  {
179  langDictId = i;
180  break;
181  }
182  }
183  }
184  if (langDictId == -1)
185  {
186  // Add in newly loaded Resource Dictionary
187  Resources.MergedDictionaries.Add(languageDictionary);
188  langDictId = Resources.MergedDictionaries.Count - 1;
189  }
190  else
191  {
192  // Replace the current langage laneTypeToRenderer with the new one
193  Resources.MergedDictionaries[langDictId] = languageDictionary;
194  }
195  }
196 
202  private void Dispatcher_UnhandledException(object sender,
203  System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
204  {
205  MessageBox.Show(e.Exception.Message, "Error",
206  MessageBoxButton.OK, MessageBoxImage.Error);
207  string path = System.IO.Path.GetDirectoryName(
208  System.Reflection.Assembly.GetExecutingAssembly().Location);
209  path += @"\err.log";
210  using (StreamWriter streamWriter = new StreamWriter(path, true))
211  {
212  Exception ex = e.Exception;
213  streamWriter.WriteLine(DateTime.Now);
214  for (int i = 0; ex != null; i++)
215  {
216  streamWriter.Write(i == 0 ? "Exception " :
217  "\r\nInner Exception " + i + ": ");
218  streamWriter.WriteLine(e.Exception.Message);
219  streamWriter.WriteLine("Call stack:");
220  streamWriter.WriteLine(e.Exception.StackTrace);
221  ex = ex.InnerException;
222  }
223  streamWriter.WriteLine("----------------------------");
224  }
225  e.Handled = true;
226  }
227  }
228 }
void Initialize(string newTopologyName, string languageOnStartup)
Creates new or open last opened geometric topology.
GUIInteractions logic for App.xaml
Definition: App.xaml.cs:17
Class implementing ISetting interface using settings of visual studio application.
Definition: Settings.cs:8
App()
Initialization of app.
Definition: App.xaml.cs:55
Class implements IInteractions interface by message boxes or dialogs.
override void OnStartup(StartupEventArgs e)
Occurs on startup of application.
Definition: App.xaml.cs:68