2013-07-27 20 views
0

所以,我有兩個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"; 
      } 

回答

8

每個IF測試後立即刪除分號。分號後面的代碼塊每次都無條件地運行。

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"; 
} 
相關問題