The pivot control has a selectedindex that can specify the currently displayed pivot Item;
I am in an application in the Monday to Sunday, altogether corresponds to seven pivot Item, and want to implement an open app to display the current date corresponding to the week page so write: SelectedIndex = (int) DateTime.Today.DayOfWeek;
The result is not the day's page, but the next day's page.
So I write the above code as SelectedIndex = ((int) datetime.today.dayofweek-1);
So after a few days, debugging up no problem.
Until today, this application in the black screen; I thought it was a problem with the code I added today, so I commented out the code we added today, and the result is a black screen.
has been tossing fast for one hours, breakpoints, debugging, or not resolved.
When I see SelectedIndex = ((int) datetime.today.dayofweek-1), this line of code suddenly realizes that the week is starting from Sunday;
Immediately checked, the original (int) DateTime.Today.DayOfWeek returned the integer is {0,1,2,3,4,5,6,}; corresponding {Sunday, Monday, ..., Friday, Saturday};
Pivot Item is also counted starting from 0, so the corresponding selectedindex in Sunday should be 6, and now the result is-1, no wonder it crashes.
And today happens to be Sunday!!! So I wasted almost one hours ...
Change to write this:
if ((int) datetime.today.dayofweek!=0) { Myclass.selectedindex = ((int) datetime.today.dayofweek-1); } else { myclass.selectedindex = 6; }
Problem solving.
Summary: When writing code, be sure to think more in advance, pay attention to the corresponding counting starting point, in order to reduce the bug (especially this Sunday bug!), improve efficiency.
WP8.1 Pivot control's SelectedIndex property vs. Sunday bug