How to pass an object from a Frame to another Frame in a Windows 8 Style App

14,914

Solution 1

Try this i hope it helps

Quickstart: Navigating between pages

Solution 2

Frame.Navigate(typeof(MainPage), nameTextBox.Text);

Then in OnNavigatedTo of MainPage

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string text = e.Parameter as string;
    if (text != null) {
        //Do your stuff
    }
}

If you want to cache your MainPage then do this

public MainPage()
    {
        this.InitializeComponent();
        //This will cache your page and every time you navigate to this 
        //page a new page will not be created.
        this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;

        this.brainPageController = new PageController();

        // add items from the List<String> to the listBox
        listGoals.ItemsSource = brainPageController.GetListGoals();
    }
Share:
14,914
Mythul
Author by

Mythul

Currently studying Computer Science at University.

Updated on August 05, 2022

Comments

  • Mythul
    Mythul over 1 year

    I have problem that i just cant figure out right now. I am trying to develop a Windows-8 style app and im stuck implementing this functionality.

    I have a MainWindow which contains a ListBox and a Button (lets say addButton).

    When i click the button i navigate to a new page, lets say AddCustomerPage with this.Frame.Navigate(typeof (AddCustomerPage));

    AddCustomerPage has 1 textBox and 1 button (lets say doneButton. When i click the button i want the string in the textBox to be added to the ListBox on the previous page.

    This is my current functionality: 1. MainWindow is created.

    1. Click addButton

    2. AddCustomer page is created. MainWindow is destroyed(problem).

    3. Click doneButton

    4. A MainWindow object is created with a ListBox with 1 item.

    5. Repeat the add process, i always get a MainWindow with a ListBox with 1 item.

    Thanks for the help. Here is the code:

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
    
            this.brainPageController = new PageController();
    
            // add items from the List<String> to the listBox
            listGoals.ItemsSource = brainPageController.GetListGoals();
        }
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var parameter = e.Parameter as String;
            // a simple controller that adds a string to a List<string>
            brainPageController.AddGoal(parameter);
        }
    
        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof (GoalsInfo));
        }
    
        // VARIABLES DECLARATION
        private PageController brainPageController;
    }
    
    public sealed partial class GoalsInfo : WinGoalsWIP.Common.LayoutAwarePage
    {
        public GoalsInfo()
        {
            this.InitializeComponent();
            this.brainPageController = new PageController();
        }
    
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }
    
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            brainPageController.AddGoal(nameTextBox.Text);
    
            this.Frame.Navigate(typeof(MainPage), nameTextBox.Text);
        }
        // VARIABLES DECLARATION
        PageController brainPageController;
    }