In my app, I have this code to display different tutorials about OneNote. This clears and resets certain values, and updates some variables so we know which version we are working with. The main problem is it has a lot of repetition in the three if
s, although all comments are welcomed.
private void SwitchVersion(string version)
{
// This programatically selects the top value that is always the same
// to prevent crashes. Items is a ListBox.
Items.SelectedValue = "OneNote";
// This clears all values except the top value.
// Data.ItemList is an ObservableCollection<string> bound to Items.
while (Data.ItemList.Count > 1)
{
Data.ItemList.RemoveAt(1);
}
// This clears the ObservableCollection<Type> of XAML pages navigated to with
// the ListBox. These values are tied to the values in ItemList.
Data.Pages.Clear();
if (version == "Phone")
{
foreach (string s in GlobalVars.PHome())
{
if (s == "OneNote")
{
continue;
}
Data.ItemList.Add(s);
}
foreach (Type t in GlobalVars.PHomeP())
{
Data.Pages.Add(t);
}
// This is the method called when a value in the ListBox is chosen.
// We need to call this to update some submenus.
Data.NewSelectionP("OneNote");
}
if(version == "WStore")
{
foreach (string s in GlobalVars.Home())
{
if (s == "OneNote") continue;
Data.ItemList.Add(s);
}
foreach (Type t in GlobalVars.HomeP()) Data.Pages.Add(t);
Data.NewSelectionWS("OneNote");
}
if (version == "2013")
{
foreach (string s in GlobalVars.Home2013())
{
if (s == "OneNote") continue;
Data.ItemList.Add(s);
}
foreach (Type t in GlobalVars.HomeP2013()) Data.Pages.Add(t);
Data.NewSelection2013("OneNote");
}
// This is the Frame used to display the data.
DataFrame.Navigate(Data.Pages[Items.SelectedIndex]);
}