简体   繁体   中英

Navigating back to Main Page in Windows Phone 8.1 app causes exception

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.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM