这个“如果”声明我做错了什么?

时间:2013-07-27 01:20:39

标签: c# windows windows-phone-8

所以,我有两个if语句,用于检测您是否使用HTC 8X或8S,使用手机提供的DeviceName。问题是它似乎忽略了“If”并且只运行了所有代码...例如,我放置了一行代码(用于调试),一旦运行每个部分就会显示一个消息框,但是两个消息框都显示出来了,而不仅仅是8X,显示它正确检测到我在8X上运行应用程序。这可能只是一个“愚蠢的错误”,但我想不出来......

  if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8X by HTC") ;
            {
                //Debugging MSG
                MessageBox.Show("8X Works")
                //Rating
                MainScore.Text = "6.1";
                //Subscores
                Processor.Text = "5.2";
                RAM.Text = "6.5";
                Graphics.Text = "8.0";
                HardDisk.Text = "5.1";
                //Issues
                Issues.Text = "0 ISSUES FOUND";
            }
            if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8S by HTC");
            {
                //Debugging MSG
                MessageBox.Show("8S Works")
                //Rating
                MainScore.Text = "2.8";
                //Subscores
                Processor.Text = "3.2";
                RAM.Text = "2.4";
                Graphics.Text = "4.0";
                HardDisk.Text = "1.9";
                //Issues
                Issues.Text = "0 ISSUES FOUND";
            }

2 个答案:

答案 0 :(得分:8)

每次IF测试后立即删除分号。分号后面的代码块每次都无条件地运行。

答案 1 :(得分:1)

由于分号,编译器会看到你的代码 (查找/* */评论)

if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8X by HTC")
    /* DO NOTHING */;

/* Regular, Unconditional Code */
{
    //Debugging MSG
    MessageBox.Show("8X Works")
    //Rating
    MainScore.Text = "6.1";
    //Subscores
    Processor.Text = "5.2";
    RAM.Text = "6.5";
    Graphics.Text = "8.0";
    HardDisk.Text = "5.1";
    //Issues
    Issues.Text = "0 ISSUES FOUND";
}

if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8S by HTC")
    /* DO NOTHING */;

/* Regular Unconditional code */
{
   //Debugging MSG
   MessageBox.Show("8S Works")
   //Rating
   MainScore.Text = "2.8";
   //Subscores
   Processor.Text = "3.2";
   RAM.Text = "2.4";
   Graphics.Text = "4.0";
   HardDisk.Text = "1.9";
   //Issues
   Issues.Text = "0 ISSUES FOUND";
 }