我可以单击一次,但是之后,它卡在了Darkmode上,我无法切换回
public static bool i = false;
private void BtnDark_Click(object sender, EventArgs e)
{
i = true;
if (i == true) // Darkmode
{
//...
i = false;
}
else if (i == false) // Whitemode
{
//...
i = true;
}
}
答案 0 :(得分:0)
这是因为您总是在方法的开头设置i = true
。此变量应在方法之外定义(并且应给它起一个适当的名称)
public static bool i = false;
private void BtnDark_Click(object sender, EventArgs e)
{
//i = true; JUST REMOVE THIS LINE
if (i) // Darkmode
{
//...
i = false;
}
else // Whitemode
{
//...
i = true;
}
}
或者这样:
if (i = !i) //assign and compare at the same time
{
//...
}
else
{
//...
}
答案 1 :(得分:0)
您做这样的事情。
public static bool i = false;
//changed the method name to be more descriptive for the event
private void BtnToggleDarkMode_Click(object sender, EventArgs e)
{
i = !i; // toggle the boolean.
//If true
if(i) {
//Do sth
}
else{
//do sth else
}
}