@@ -78,7 +78,8 @@ public async Task InitializeAsync()
78
78
{
79
79
if ( type == "checkbox" )
80
80
{
81
- Settings [ attributes . Name ] = bool . Parse ( attributes . DefaultValue ) ;
81
+ // If can parse the default value to bool, use it, otherwise use false
82
+ Settings [ attributes . Name ] = bool . TryParse ( attributes . DefaultValue , out var value ) && value ;
82
83
}
83
84
else
84
85
{
@@ -105,19 +106,24 @@ public void UpdateSettings(IReadOnlyDictionary<string, object> settings)
105
106
switch ( control )
106
107
{
107
108
case TextBox textBox :
108
- textBox . Dispatcher . Invoke ( ( ) => textBox . Text = value as string ?? string . Empty ) ;
109
+ var text = value as string ?? string . Empty ;
110
+ textBox . Dispatcher . Invoke ( ( ) => textBox . Text = text ) ;
109
111
break ;
110
112
case PasswordBox passwordBox :
111
- passwordBox . Dispatcher . Invoke ( ( ) => passwordBox . Password = value as string ?? string . Empty ) ;
113
+ var password = value as string ?? string . Empty ;
114
+ passwordBox . Dispatcher . Invoke ( ( ) => passwordBox . Password = password ) ;
112
115
break ;
113
116
case ComboBox comboBox :
114
117
comboBox . Dispatcher . Invoke ( ( ) => comboBox . SelectedItem = value ) ;
115
118
break ;
116
119
case CheckBox checkBox :
120
+ var isChecked = value is bool boolValue
121
+ ? boolValue
122
+ // If can parse the default value to bool, use it, otherwise use false
123
+ : value is string stringValue && bool . TryParse ( stringValue , out var boolValueFromString )
124
+ && boolValueFromString ;
117
125
checkBox . Dispatcher . Invoke ( ( ) =>
118
- checkBox . IsChecked = value is bool isChecked
119
- ? isChecked
120
- : bool . Parse ( value as string ?? string . Empty ) ) ;
126
+ checkBox . IsChecked = isChecked ) ;
121
127
break ;
122
128
}
123
129
}
@@ -145,6 +151,7 @@ public bool NeedCreateSettingPanel()
145
151
public Control CreateSettingPanel ( )
146
152
{
147
153
// No need to check if NeedCreateSettingPanel is true because CreateSettingPanel will only be called if it's true
154
+ // if (!NeedCreateSettingPanel()) return null;
148
155
149
156
// Create main grid with two columns (Column 1: Auto, Column 2: *)
150
157
var mainPanel = new Grid { Margin = SettingPanelMargin , VerticalAlignment = VerticalAlignment . Center } ;
@@ -159,7 +166,7 @@ public Control CreateSettingPanel()
159
166
160
167
// Iterate over each setting and create one row for it
161
168
var rowCount = 0 ;
162
- foreach ( var ( type , attributes ) in Configuration . Body )
169
+ foreach ( var ( type , attributes ) in Configuration ! . Body )
163
170
{
164
171
// Skip if the setting does not have attributes or name
165
172
if ( attributes ? . Name == null )
@@ -389,12 +396,14 @@ public Control CreateSettingPanel()
389
396
}
390
397
case "checkbox" :
391
398
{
399
+ // If can parse the default value to bool, use it, otherwise use false
400
+ var defaultValue = bool . TryParse ( attributes . DefaultValue , out var value ) && value ;
392
401
var checkBox = new CheckBox
393
402
{
394
403
IsChecked =
395
404
Settings [ attributes . Name ] is bool isChecked
396
405
? isChecked
397
- : bool . Parse ( attributes . DefaultValue ) ,
406
+ : defaultValue ,
398
407
HorizontalAlignment = HorizontalAlignment . Left ,
399
408
VerticalAlignment = VerticalAlignment . Center ,
400
409
Margin = SettingPanelItemTopBottomMargin ,
@@ -404,7 +413,7 @@ Settings[attributes.Name] is bool isChecked
404
413
405
414
checkBox . Click += ( sender , _ ) =>
406
415
{
407
- Settings [ attributes . Name ] = ( ( CheckBox ) sender ) . IsChecked ! ;
416
+ Settings [ attributes . Name ] = ( ( CheckBox ) sender ) . IsChecked ?? defaultValue ;
408
417
} ;
409
418
410
419
contentControl = checkBox ;
0 commit comments