55using Microsoft . UI . Xaml . Controls ;
66using Microsoft . UI . Xaml . Media ;
77using Microsoft . UI . Xaml . Navigation ;
8- using SimpleWeather . Controls ;
98using SimpleWeather . Extras ;
109using SimpleWeather . NET . BackgroundTasks ;
1110using SimpleWeather . NET . Controls ;
2120using SimpleWeather . Weather_API . WeatherData ;
2221using SimpleWeather . WeatherData ;
2322using System . Globalization ;
24- using System . Linq ;
2523using Windows . ApplicationModel ;
2624using Windows . Devices . Geolocation ;
2725using ComboBoxItem = SimpleWeather . Controls . ComboBoxItem ;
28- using ResStrings = SimpleWeather . Resources . Strings . Resources ;
2926
3027namespace SimpleWeather . NET . Preferences
3128{
@@ -71,21 +68,6 @@ public Settings_General()
7168
7269 RestoreSettings ( ) ;
7370
74- // Event Listeners
75- FollowGPS . Toggled += FollowGPS_Toggled ;
76- AlertSwitch . Toggled += AlertSwitch_Toggled ;
77- APIComboBox . SelectionChanged += APIComboBox_SelectionChanged ;
78- RefreshComboBox . SelectionChanged += RefreshComboBox_SelectionChanged ;
79- RadarComboBox . SelectionChanged += RadarComboBox_SelectionChanged ;
80- PersonalKeySwitch . Toggled += PersonalKeySwitch_Toggled ;
81- ThemeComboBox . SelectionChanged += ThemeComboBox_SelectionChanged ;
82- LanguageComboBox . SelectionChanged += LanguageComboBox_SelectionChanged ;
83- DailyNotifSwitch . Toggled += DailyNotifSwitch_Toggled ;
84- DailyNotifTimePicker . SelectedTimeChanged += DailyNotifTimePicker_SelectedTimeChanged ;
85- PoPChanceNotifSwitch . Toggled += PoPChanceNotifSwitch_Toggled ;
86- PoPChancePct . SelectionChanged += PoPChancePct_SelectionChanged ;
87- MinAlertSeverity . SelectionChanged += MinAlertSeverity_SelectionChanged ;
88-
8971 AnalyticsLogger . LogEvent ( "Settings_General" ) ;
9072 }
9173
@@ -238,6 +220,40 @@ private void RestoreSettings()
238220 } ) ;
239221 }
240222
223+ private void RegisterListeners ( )
224+ {
225+ FollowGPS . Toggled += FollowGPS_Toggled ;
226+ AlertSwitch . Toggled += AlertSwitch_Toggled ;
227+ APIComboBox . SelectionChanged += APIComboBox_SelectionChanged ;
228+ RefreshComboBox . SelectionChanged += RefreshComboBox_SelectionChanged ;
229+ RadarComboBox . SelectionChanged += RadarComboBox_SelectionChanged ;
230+ PersonalKeySwitch . Toggled += PersonalKeySwitch_Toggled ;
231+ ThemeComboBox . SelectionChanged += ThemeComboBox_SelectionChanged ;
232+ LanguageComboBox . SelectionChanged += LanguageComboBox_SelectionChanged ;
233+ DailyNotifSwitch . Toggled += DailyNotifSwitch_Toggled ;
234+ DailyNotifTimePicker . SelectedTimeChanged += DailyNotifTimePicker_SelectedTimeChanged ;
235+ PoPChanceNotifSwitch . Toggled += PoPChanceNotifSwitch_Toggled ;
236+ PoPChancePct . SelectionChanged += PoPChancePct_SelectionChanged ;
237+ MinAlertSeverity . SelectionChanged += MinAlertSeverity_SelectionChanged ;
238+ }
239+
240+ private void UnregisterListeners ( )
241+ {
242+ FollowGPS . Toggled -= FollowGPS_Toggled ;
243+ AlertSwitch . Toggled -= AlertSwitch_Toggled ;
244+ APIComboBox . SelectionChanged -= APIComboBox_SelectionChanged ;
245+ RefreshComboBox . SelectionChanged -= RefreshComboBox_SelectionChanged ;
246+ RadarComboBox . SelectionChanged -= RadarComboBox_SelectionChanged ;
247+ PersonalKeySwitch . Toggled -= PersonalKeySwitch_Toggled ;
248+ ThemeComboBox . SelectionChanged -= ThemeComboBox_SelectionChanged ;
249+ LanguageComboBox . SelectionChanged -= LanguageComboBox_SelectionChanged ;
250+ DailyNotifSwitch . Toggled -= DailyNotifSwitch_Toggled ;
251+ DailyNotifTimePicker . SelectedTimeChanged -= DailyNotifTimePicker_SelectedTimeChanged ;
252+ PoPChanceNotifSwitch . Toggled -= PoPChanceNotifSwitch_Toggled ;
253+ PoPChancePct . SelectionChanged -= PoPChancePct_SelectionChanged ;
254+ MinAlertSeverity . SelectionChanged -= MinAlertSeverity_SelectionChanged ;
255+ }
256+
241257 public Task < bool > OnBackRequested ( )
242258 {
243259 var provider = APIComboBox . SelectedValue . ToString ( ) ;
@@ -261,6 +277,8 @@ public void OnNavigatedToPage(NavigationEventArgs e)
261277 App . Current . UnregisterSettingsListener ( ) ;
262278 SettingsManager . OnSettingsChanged += Settings_OnSettingsChanged ;
263279 RestoreSettings ( ) ;
280+ // Event Listeners
281+ RegisterListeners ( ) ;
264282 }
265283
266284 public void OnNavigatedFromPage ( NavigationEventArgs e )
@@ -279,9 +297,10 @@ public void OnNavigatingFromPage(NavigatingCancelEventArgs e)
279297 }
280298 else
281299 {
282- // Unsubscribe from event
300+ // Unsubscribe from events
283301 App . Current . RegisterSettingsListener ( ) ;
284302 SettingsManager . OnSettingsChanged -= Settings_OnSettingsChanged ;
303+ UnregisterListeners ( ) ;
285304
286305 ProcessQueue ( ) ;
287306 }
@@ -390,6 +409,8 @@ private Task ShowKeyEntryDialog()
390409 {
391410 return DispatcherQueue . EnqueueAsync ( async ( ) =>
392411 {
412+ if ( this . XamlRoot == null ) return ;
413+
393414 var keydialog = new KeyEntryDialog ( APIComboBox . SelectedValue . ToString ( ) )
394415 {
395416 RequestedTheme = Shell . Instance . AppFrame . RequestedTheme ,
0 commit comments