diff --git a/NetTally/MainWindow.xaml.cs b/NetTally/MainWindow.xaml.cs index a7eb4dad..c0007fe7 100644 --- a/NetTally/MainWindow.xaml.cs +++ b/NetTally/MainWindow.xaml.cs @@ -58,45 +58,53 @@ public string MyTitle /// public MainWindow() { - // Set up an event handler for any otherwise unhandled exceptions in the code. - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + try + { + // Set up an event handler for any otherwise unhandled exceptions in the code. + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; - InitializeComponent(); + InitializeComponent(); - // Set tally vars - tally = new Tally(); + // Set tally vars + tally = new Tally(); - questCollection = new QuestCollection(); + questCollection = new QuestCollection(); - QuestCollectionWrapper wrapper = new QuestCollectionWrapper(questCollection, null, DisplayMode.Normal); + QuestCollectionWrapper wrapper = new QuestCollectionWrapper(questCollection, null, DisplayMode.Normal); - NetTallyConfig.Load(tally, wrapper); + NetTallyConfig.Load(tally, wrapper); - // Set up view for binding - QuestCollectionView = CollectionViewSource.GetDefaultView(questCollection); - // Sort the collection view - var sortDesc = new SortDescription("DisplayName", ListSortDirection.Ascending); - QuestCollectionView.SortDescriptions.Add(sortDesc); - // Set the current item - QuestCollectionView.MoveCurrentTo(questCollection[wrapper.CurrentQuest]); + // Set up view for binding + QuestCollectionView = CollectionViewSource.GetDefaultView(questCollection); + // Sort the collection view + var sortDesc = new SortDescription("DisplayName", ListSortDirection.Ascending); + QuestCollectionView.SortDescriptions.Add(sortDesc); + // Set the current item + QuestCollectionView.MoveCurrentTo(questCollection[wrapper.CurrentQuest]); - Properties.Settings settings = new Properties.Settings(); - tally.DisplayMode = wrapper.DisplayMode; + Properties.Settings settings = new Properties.Settings(); + tally.DisplayMode = wrapper.DisplayMode; - // Set up data contexts - DataContext = QuestCollectionView; + // Set up data contexts + DataContext = QuestCollectionView; - resultsWindow.DataContext = tally; - tallyButton.DataContext = tally; - cancelTally.DataContext = tally; - displayMode.DataContext = tally; - newRelease.DataContext = checkForNewRelease; + resultsWindow.DataContext = tally; + tallyButton.DataContext = tally; + cancelTally.DataContext = tally; + displayMode.DataContext = tally; + newRelease.DataContext = checkForNewRelease; - var assembly = Assembly.GetExecutingAssembly(); - var product = (AssemblyProductAttribute)assembly.GetCustomAttribute(typeof(AssemblyProductAttribute)); - var version = (AssemblyInformationalVersionAttribute)assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute)); - MyTitle = $"{product.Product} - {version.InformationalVersion}"; + var assembly = Assembly.GetExecutingAssembly(); + var product = (AssemblyProductAttribute)assembly.GetCustomAttribute(typeof(AssemblyProductAttribute)); + var version = (AssemblyInformationalVersionAttribute)assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute)); + MyTitle = $"{product.Product} - {version.InformationalVersion}"; + } + catch (Exception e) + { + string file = ErrorLog.Log(e); + MessageBox.Show($"Error log saved to:\n{file ?? "(unable to write log file)"}", "Error in startup", MessageBoxButton.OK, MessageBoxImage.Error); + } } /// @@ -111,7 +119,7 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE string file = ErrorLog.Log(ex); - MessageBox.Show($"Error log written to:\n{file ?? "(unable to write log file)"}", "Unhandled exception", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show($"Error log saved to:\n{file ?? "(unable to write log file)"}", "Unhandled exception", MessageBoxButton.OK, MessageBoxImage.Error); } /// @@ -121,18 +129,26 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE /// private void Window_Closing(object sender, CancelEventArgs e) { - string selectedQuest = ""; - - if (CurrentlySelectedQuest() != null) + try { - selectedQuest = CurrentlySelectedQuest().ThreadName; - } + string selectedQuest = ""; + + if (CurrentlySelectedQuest() != null) + { + selectedQuest = CurrentlySelectedQuest().ThreadName; + } - QuestCollectionWrapper qcw = new QuestCollectionWrapper(questCollection, selectedQuest, tally.DisplayMode); - NetTallyConfig.Save(tally, qcw); + QuestCollectionWrapper qcw = new QuestCollectionWrapper(questCollection, selectedQuest, tally.DisplayMode); + NetTallyConfig.Save(tally, qcw); - Properties.Settings settings = new Properties.Settings(); - settings.Save(); + Properties.Settings settings = new Properties.Settings(); + settings.Save(); + } + catch(Exception ex) + { + string file = ErrorLog.Log(ex); + MessageBox.Show($"Error log saved to:\n{file ?? "(unable to write log file)"}", "Error in shutdown", MessageBoxButton.OK, MessageBoxImage.Error); + } } #endregion