如何将对象从帧传递到Windows 8样式应用程序中的另一个帧

Myt*_*hul 2 c# windows-8 windows-store-apps

我有问题,我现在无法弄清楚.我正在尝试开发一个Windows-8风格的应用程序,并且我坚持实现这个功能.

我有一个MainWindow,它包含一个ListBox和一个Button(比如说addButton).

当我单击按钮导航到新页面时,让我们说AddCustomerPage with this.Frame.Navigate(typeof(AddCustomerPage));

AddCustomerPage有1个textBox和1个按钮(比如doneButton.当我点击按钮时,我希望textBox中的字符串被添加到上一页的ListBox中.

这是我目前的功能:1.MainWindow已创建.

  1. 单击addButton

  2. AddCustomer页面已创建.MainWindow被破坏(问题).

  3. 点击doneButton

  4. 使用带有1个项目的ListBox创建MainWindow对象.

  5. 重复添加过程,我总是得到一个带有1个项目的ListBox的MainWindow.

谢谢您的帮助.这是代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

Ind*_*ore 11

Frame.Navigate(typeof(MainPage), nameTextBox.Text);
Run Code Online (Sandbox Code Playgroud)

然后在OnNavigatedTo MainPage

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

如果要缓存MainPage,请执行此操作

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();
    }
Run Code Online (Sandbox Code Playgroud)