2
votes

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.

2
What is the exception? As an aside I worked on a commercial Windows Phone app where the pages to navigate to were simply controls which became active on the main page. The back button press was simply to change the visibility to hidden.ΩmegaMan
I'm not sure how to get the actual exception. Whenever I try to navigate back, the app crashes and VS goes to the "App.g.i.cs" page and highlights "#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION UnhandledException += (sender, e) => { if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); }; #endif" Is there somewhere that I can get a better description of the exception?Eric Stevens
"> GuidedWorkout.exe!GuidedWorkout.App.InitializeComponent.AnonymousMethod__2(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e) Line 50 C#"Eric Stevens
You need to give us e parameter's details.Code you provided us here doesn't help much since it's a general Unhandled Exception snippet.Burak Kaan Köse
Do not put the code in comments, you can actively edit your post with that information. Then simply answer back to someone by calling them out, in the comment such as "@omegaman check out my updated post" as a comment answer.ΩmegaMan

2 Answers

0
votes

Why not just use

    Frame.GoBack();

Btw I hope you do realize, when you AddWorkOut on the second page and add it to a List in MainPage, its just saving it in-memory for that session. The moment app is closed, you will lose the data added.

I will recommend you to save/read the data from a more persistent form like

It is as simple as :

private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

// to add
userSettings.Add("testdata"); 
userSettings.Save();

//to read
string xx = userSettings[“SomeValue”].ToString();

If you want to scale, consider other ways like a database.

0
votes

Usse a hyperlink button, instead of a Button.

xaml:

<HyperlinkButton Content="My Label"            
                 Click="HyperlinkButton_Click" />

cs:

// Navigate to MainPage.
private void HyperlinkButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
    // TO DO
    this.Frame.Navigate(typeof(MainPage));
}

Use this in conjunction with the OnNavigatedTo method for MainPage, as required.

Also, if you are wanting to update the page whenever you return to it:

// Dont want to keep cached data.
this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Disabled;