Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将复选框/单选按钮布尔保存到全局变量_C#_Wpf_Global Variables_Boolean - Fatal编程技术网

C# 将复选框/单选按钮布尔保存到全局变量

C# 将复选框/单选按钮布尔保存到全局变量,c#,wpf,global-variables,boolean,C#,Wpf,Global Variables,Boolean,使用WPF应用程序 当我想将bool保存到全局变量时,会出现如下错误: Window msgOne = new Window(); if (Properties.Settings.Default.sound01 == true) { msgOne.radioboxSound.IsChecked = true; } else { msgOne.radioboxSound.IsChecked = false; } msgOne.ShowDialog(); //wa

使用WPF应用程序 当我想将bool保存到全局变量时,会出现如下错误:

Window msgOne = new Window();
 if (Properties.Settings.Default.sound01 == true)
  {
    msgOne.radioboxSound.IsChecked = true;
  }
 else
  {
    msgOne.radioboxSound.IsChecked = false;
  }
msgOne.ShowDialog();
//waiting for close
Properties.Settings.Default.sound01 = msgOne.radioboxSound.IsChecked;
其中radioboxSound是复选框,Properties.Settings.Default.sound01是全局参数,您可以在VB c的“设置”选项卡中设置该参数

我在上面snipit的最后一个链接中遇到的错误: 无法隐式转换类型bool?去布尔。存在显式转换。是否缺少强制转换

但我没有使用任何bool?,只有已经设置的BOLL

有人知道怎么处理这件事吗


thanx

单选按钮的IsChecked属性是“bool”。将代码更改为

Properties.Settings.Default.sound01 = msgOne.radioboxSound.IsChecked??false;
再解释一下

由于RadioButton.IsChecked是可空的,因此无论IsThrestate属性设置为true还是false,您都需要处理IsChecked可空的事实

你可以用

Properties.Settings.Default.sound01 = msgOne.radioboxSound.IsChecked == true;

或者如上所述使用空合并运算符。

单选按钮的IsChecked属性是“bool”。将代码更改为

Properties.Settings.Default.sound01 = msgOne.radioboxSound.IsChecked??false;
再解释一下

由于RadioButton.IsChecked是可空的,因此无论IsThrestate属性设置为true还是false,您都需要处理IsChecked可空的事实

你可以用

Properties.Settings.Default.sound01 = msgOne.radioboxSound.IsChecked == true;

或者如上所述使用空合并运算符。

是否使用可空的aka bool?因为IsChecked是一个。你使用了一个可空的aka bool吗?因为IsChecked是一个。这确实解决了它,但是你能解释一下我的理解吗?“false->”的意思是,如果它以某种方式无法获取它,它将设置为false?或者它能做什么?没错。它是空合并运算符,有效地简化了:msgOne.radioboxSound.IsChecked.HasValue?msgOne.radioboxSound.IsChecked.Value:false;这本身就是if…..的缩写。。。否则…这确实解决了问题,但你能解释一下我的理解吗?“false->”的意思是,如果它以某种方式无法获取它,它将设置为false?或者它能做什么?没错。它是空合并运算符,有效地简化了:msgOne.radioboxSound.IsChecked.HasValue?msgOne.radioboxSound.IsChecked.Value:false;这本身就是if…..的缩写。。。其他的