2014-10-29 66 views
-1

我有5個按鈕,它們通過顯示忽略星期六和星期日的星期幾名稱來動態更改。我可以使用Button.Content在Windows Phone中實現此目的嗎?Windows Phone App 7.1更改後面代碼的按鈕文本

現在我嘗試運行它,但是當我想要轉到包含這些按鈕的屏幕時,它不會加載但凍結並且不會輸出。

我在想什麼或做錯了什麼?

這裏是我的代碼:

 DateTime datetoday = DateTime.Today; 

     for (int i = 0; i < 5;) 
     { 

      if (datetoday.DayOfWeek == DayOfWeek.Sunday || datetoday.DayOfWeek == DayOfWeek.Saturday) 
      { 
       //it's saturday of sunday 
      } 
      else 
      { 
       if (i == 0) 
       { 
        if (datetoday.DayOfWeek == DayOfWeek.Friday) 
        { 
         firstButton.Content = "Vrijdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Thursday) 
        { 
         firstButton.Content = "Donderdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Wednesday) 
        { 
         firstButton.Content = "Woensdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Tuesday) 
        { 
         firstButton.Content = "Dinsdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Monday) 
        { 
         firstButton.Content = "Maandag"; 
        } 
       } 

       if (i == 1) 
       { 
        if (datetoday.DayOfWeek == DayOfWeek.Friday) 
        { 
         secondButton.Content = "Vrijdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Thursday) 
        { 
         secondButton.Content = "Donderdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Wednesday) 
        { 
         secondButton.Content = "Woensdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Tuesday) 
        { 
         secondButton.Content = "Dinsdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Monday) 
        { 
         secondButton.Content = "Maandag"; 
        } 
       } 
       if (i == 2) 
       { 
        if (datetoday.DayOfWeek == DayOfWeek.Friday) 
        { 
         thirdButton.Content = "Vrijdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Thursday) 
        { 
         thirdButton.Content = "Donderdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Wednesday) 
        { 
         thirdButton.Content = "Woensdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Tuesday) 
        { 
         thirdButton.Content = "Dinsdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Monday) 
        { 
         thirdButton.Content = "Maandag"; 
        } 
       } 
       if (i == 3) 
       { 
        if (datetoday.DayOfWeek == DayOfWeek.Friday) 
        { 
         fourthButton.Content = "Vrijdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Thursday) 
        { 
         fourthButton.Content = "Donderdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Wednesday) 
        { 
         fourthButton.Content = "Woensdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Tuesday) 
        { 
         fourthButton.Content = "Dinsdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Monday) 
        { 
         fourthButton.Content = "Maandag"; 
        } 
       } 
       if (i == 4) 
       { 
        if (datetoday.DayOfWeek == DayOfWeek.Friday) 
        { 
         fifthButton.Content = "Vrijdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Thursday) 
        { 
         fifthButton.Content = "Donderdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Wednesday) 
        { 
         fifthButton.Content = "Woensdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Tuesday) 
        { 
         fifthButton.Content = "Dinsdag"; 
        } 
        if (datetoday.DayOfWeek == DayOfWeek.Monday) 
        { 
         fifthButton.Content = "Maandag"; 
        } 
       } 
       //currentWeek[i] = datetoday; 
       // i++; 
      } 
      // datetoday = datetoday.AddDays(1); 
     } 
    } 
} 

}

+0

BRB,thedailywtf.com – Will 2014-10-29 14:20:03

回答

0

這凍結,因爲永遠循環運行 - i從不增加,因爲你comented出i++線。

您的代碼可以大大簡化。你可以達到同樣具有:

var buttons = new [] {firstButton, secondButton, thirdButton, fourthButton, fifthButton}; 
var date = DateTime.Now; 
foreach (var button in buttons) 
{ 
    while(date.DayOfWeek == DayOfWeek.Sunday || date.DayOfWeek == DayOfWeek.Saturday) 
     date = date.AddDays(1); 
    button.Content = date.ToString("dddd"); 
    date = date.AddDays(1); 
} 

,或者,如果您需要在特定的語言名稱,用途:

​​
+0

太謝謝你了!你應得一枚獎牌! – Dave1991NL 2014-10-29 13:35:20

相關問題