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;
}
}
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 { //... }
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
}
}