[英]How to check and change name button in C# Windows Phone
I have a problem, I'm making an easy game noughts and crosses on Windows Phone with Buttons: 我有一个问题,我正在制作一个简单的游戏noughts并在带有按钮的Windows Phone上进行交叉:
<Button x:Name="ba0" Content=" " HorizontalAlignment="Left" Height="126" Margin="37,146,0,0" VerticalAlignment="Top" Width="126" Click="a0"/>
I would like to change the content of the button if on the button there's " " and not to change if there's "X" or "O". 我想改变按钮的内容,如果在按钮上有“”,如果有“X”或“O”则不改变。 I wrote something like that:
我写了类似的东西:
public void a0(object sender, System.EventArgs e)
{
if (ba0.Content == " ")
ba0.Content = "X";
else
return;
}
But it doesn't work properly. 但它不能正常工作。 Any idea?
任何想法?
Try following code: 请尝试以下代码:
<Button x:Name="ba0" HorizontalAlignment="Left" Height="126" Margin="37,146,0,0" VerticalAlignment="Top" Width="126" Click="a0"/>
public void a0(object sender, System.EventArgs e)
{
if (string.IsNullOrEmpty(Convert.ToString(ba0.Content)))
ba0.Content = "X";
else
return;
}
Try the following 请尝试以下方法
Convert the content into string type and also trim the value.
将内容转换为字符串类型,并修剪值。
public void a0(object sender, System.EventArgs e)
{
if (string.IsNullOrEmpty(Convert.ToString(ba0.Content).Trim()))
ba0.Content = "X";
else
return;
}
or check for white spaces
或检查空白区域
public void a0(object sender, System.EventArgs e)
{
var content = Convert.ToString(ba0.Content);
if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content))
ba0.Content = "X";
else
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.