I'm tinkering around for the first time with creating Windows Phone apps, and I'm having trouble with something.
My program has two pages- the main page has a list of workouts (which is pre-populated for now) and three buttons. When the "add" button is pressed, you are navigated to a second page, which has a textbox and another "add" button. I am attempting to make the add button update the workout list (which seems to work) and navigate back to MainPage.
I've tried two ways to accomplish this - I added in "this.Frame.Navigate(typeof(MainPage));" to the add button. I've also added a "back button pressed" handler to go back to the last page.
Pressing the "added" button when the navigate code is enabled causes an exception every time. If you navigate to the page and hit the back button before hitting the add button, the page goes back without an issues. However, if you hit "add" and then try to go back, it causes an exception again.
Here's the code for the main page.
namespace GuidedWorkout
{
public sealed partial class MainPage : Page
{
public static List<string> allWorkouts = new List<string>();
public string textInfo { get; set; }
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
Workouts Workout = new Workouts("Chest Day");
Workouts Workout3 = new Workouts("Leg Day");
Workouts workout2 = new Workouts("Arms Day");
allWorkoutsList.ItemsSource = allWorkouts;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//this.allWorkoutsList.ItemContainerGenerator.ContainerFromItem();
//textInfo = (sender as ListBox).SelectedItem.ToString();
//textInfo = lbi.Content.ToString();
//this.titleBlock.Text = textInfo;
}
private void addWorkoutButton_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(GuidedWorkout.AddWorkout));
}
}
And here's the code for the second page.
namespace GuidedWorkout
{
public sealed partial class AddWorkout : Page
{
public AddWorkout()
{
this.InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if(rootFrame != null && rootFrame.CanGoBack)
{
rootFrame.GoBack();
e.Handled = true;
}
}
private void addWorkoutButton_Tapped(object sender, TappedRoutedEventArgs e)
{
string wName;
wName = workoutTextBox.Text.ToString();
Workouts addedWorkout = new Workouts(wName);
MainPage.allWorkouts.Add(addedWorkout.workoutName);
//this.Frame.Navigate(typeof(GuidedWorkout.MainPage));
}
}
}
Thanks for the help.