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

Posted by Mythul on Stack Overflow See other posts from Stack Overflow or by Mythul
Published on 2012-12-15T09:02:23Z Indexed on 2012/12/15 11:04 UTC
Read the original article Hit count: 139

Filed under:
|
|

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;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-8