如何将对象从帧传递到Windows 8样式应用程序中的另一个帧
我有问题,我现在无法弄清楚。 我正在尝试开发一个Windows-8风格的应用程序,并且我坚持实现这个function。
我有一个MainWindow ,它包含一个ListBox和一个Button(比如说addButton )。
当我单击按钮导航到新页面时,让我们说AddCustomerPage with this.Frame.Navigate(typeof(AddCustomerPage));
AddCustomerPage有1个textBox和1个按钮(比如doneButton 。当我点击按钮时,我希望textBox中的字符串被添加到上一页的ListBox中。
这是我目前的function:1。MainWindow已创建。
-
单击addButton
-
AddCustomer页面已创建。 MainWindow被破坏(问题)。
-
点击doneButton
-
使用带有1个项目的ListBox创建MainWindow对象。
-
重复添加过程,我总是得到一个带有1个项目的ListBox的MainWindow。
谢谢您的帮助。 这是代码:
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); this.brainPageController = new PageController(); // add items from the List 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 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 pageState) { } protected override void SaveState(Dictionary 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; }
试试这个我希望它有所帮助
快速入门:在页面之间导航
Frame.Navigate(typeof(MainPage), nameTextBox.Text);
然后在OnNavigatedTo的MainPage
protected override void OnNavigatedTo(NavigationEventArgs e) { string text = e.Parameter as string; if (text != null) { //Do your stuff } }
如果要缓存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 to the listBox listGoals.ItemsSource = brainPageController.GetListGoals(); }