/// <summary> /// Function that's run when the program first starts. /// Set up the data context links with the local variables. /// </summary> public MainWindow() { try { // Set up an event handler for any otherwise unhandled exceptions in the code. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; InitializeComponent(); Title = $"{ProductInfo.Name} - {ProductInfo.Version}"; // Load configuration data QuestCollectionWrapper config; try { config = NetTallyConfig.Load(); } catch (ConfigurationErrorsException e) { MessageBox.Show(e.Message, "Error in configuration. Current configuration ignored.", MessageBoxButton.OK, MessageBoxImage.Error); config = null; } PlatformSetup(config); } catch (Exception e) { ErrorLog.Log(e); MessageBox.Show(e.Message, "Unable to start up. Closing.", MessageBoxButton.OK, MessageBoxImage.Error); this.Close(); } }
/// <summary> /// Function that's run when the program first starts. /// Set up the data context links with the local variables. /// </summary> public MainWindow(ViewModel model, IoCNavigationService navigationService, ILogger <MainWindow> logger) { // Initialize the readonly fields. this.mainViewModel = model; this.navigationService = navigationService; this._syncContext = SynchronizationContext.Current !; this.logger = logger; try { // Set up an event handler for any otherwise unhandled exceptions in the code. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; // Initialize the window. InitializeComponent(); // Set the title. Title = $"{ProductInfo.Name} - {ProductInfo.Version}"; // Load configuration data QuestCollection?quests = null; string? currentQuest = null; try { logger.LogDebug("Loading configuration."); NetTallyConfig.Load(out quests, out currentQuest, AdvancedOptions.Instance); logger.LogInformation("Configuration loaded."); } catch (ConfigurationErrorsException e) { MessageBox.Show(e.Message, "Error in configuration. Current configuration ignored.", MessageBoxButton.OK, MessageBoxImage.Error); } // Complete the platform setup. PlatformSetup(quests, currentQuest); } catch (Exception e) { logger.LogError(e, "Failure during program startup."); ShowWarning("Unable to start the program.", "Failure on startup"); this.Close(); } }
/// <summary> /// Saves the configuration. /// </summary> private void SaveConfig() { try { if (mainViewModel == null) { return; } string selectedQuest = mainViewModel.SelectedQuest?.ThreadName ?? ""; NetTallyConfig.Save(mainViewModel.QuestList, selectedQuest, AdvancedOptions.Instance); } catch (Exception ex) { SaveExceptionAndNotifyUser(ex); } }
/// <summary> /// Function that's run when the program first starts. /// Set up the data context links with the local variables. /// </summary> public MainWindow() { try { _syncContext = SynchronizationContext.Current; // Set up an event handler for any otherwise unhandled exceptions in the code. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; // Set up the logger to use the Windows error log. Logger.LogUsing(new WindowsErrorLog()); // Initialize the window. InitializeComponent(); // Set the title. Title = $"{ProductInfo.Name} - {ProductInfo.Version}"; // Load configuration data QuestCollection quests = null; string currentQuest = null; try { NetTallyConfig.Load(out quests, out currentQuest, AdvancedOptions.Instance); } catch (ConfigurationErrorsException e) { MessageBox.Show(e.Message, "Error in configuration. Current configuration ignored.", MessageBoxButton.OK, MessageBoxImage.Error); } // Complete the platform setup. PlatformSetup(quests, currentQuest); } catch (Exception e) { Logger.Error("Failure during program startup.", e); MessageBox.Show(e.Message, "Unable to start up. Closing.", MessageBoxButton.OK, MessageBoxImage.Error); this.Close(); } }
/// <summary> /// When the program closes, save the current list of quests. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Closing(object sender, CancelEventArgs e) { try { if (ViewModelService.MainViewModel == null) { return; } string selectedQuest = ViewModelService.MainViewModel.SelectedQuest?.ThreadName ?? ""; QuestCollectionWrapper wrapper = new QuestCollectionWrapper(ViewModelService.MainViewModel.QuestList, selectedQuest); NetTallyConfig.Save(wrapper); } 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); } }
/// <summary> /// Saves the configuration. /// </summary> private void SaveConfig() { try { if (mainViewModel == null) { return; } string selectedQuest = mainViewModel.SelectedQuest?.ThreadName ?? ""; NetTallyConfig.Save(mainViewModel.QuestList, selectedQuest, AdvancedOptions.Instance); logger.LogDebug("Configuration saved."); } catch (Exception ex) { logger.LogWarning(ex, "Failed to save configuration."); ShowWarning("The program failed to save configuration data.", "Failed to save configuration"); } }