2016-02-17 56 views
0

假设我想通过多个页面传递一个对象(引用)。我可以通过​​导航并传递参数。但是如何正确地将回参考点传回来?UWP - 在页面之间传递参数的正确方法

protected override void OnNavigatedTo(NavigationEventArgs e) { 
    if (e.Parameter is SomeClass) { 
     this.someObject = (SomeClass)e.Parameter; 
    } 
    else { 
     this.someObject = new SomeClass(); 
    } 

    SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; 
    SystemNavigationManager.GetForCurrentView().BackRequested += OnHardwareButtonsBackPressed; 

    base.OnNavigatedTo(e); 
} 


private void OnHardwareButtonsBackPressed(object sender, BackRequestedEventArgs e) { 
    // This is the missing line! 
    Frame.Navigate(typeof(FirstPage), this.someObject); 
} 

但是当我按后退按钮就可以追溯到FirstPageOnNavigatedTo不带参数,然后回到SecondPageOnHardwareButtonsBackPressed,然后回到FirstPageOnNavigatedTo与填充参数。

你能否请教我一些更好的方法?

回答

3

在您的后台处理程序中,请不要再向前导航,只需拨打GoBack - 如果您在全局级别而不是页面级别处理该问题,通常会更轻松。

您可以在全局/静态对象中存储您的应用程序状态(您希望在页面导航中保留的事物),也可以直接修改从初始导航传递的对象(如果调用页面仍有引用,它将能够看到更改)。

我会考虑搜索“MVVM Windows应用程序”并查看一些结果以了解构建XAML应用程序的常见方法。