【发布时间】:2023-04-05 00:08:02
【问题描述】:
在我使用 MVVM 模式的 Windows Phone 8.1 应用程序中,我想做页面导航。我为此编写了一个代码。并且前进页面导航工作正常!
但我面临的问题是在页面上按下后退按钮时,应用程序正在退出。
我认为问题在于我每次在页面导航期间都会创建一个新的_frame 对象,因此,_frame 对象不会保留导航历史记录。即使经过几轮导航,_frame.CanGoBack 也会给出false(应该是true)。
那么我在这里错过了什么?如何保存导航历史?我的方法有什么问题吗?我将不胜感激。
代码如下:
导航服务类:
public class NavigationService : INavigationService
{
private readonly Frame _frame;
public NavigationService()
{
_frame = new Frame();
}
public void NavigateTo(Type source)
{
Window.Current.Content = _frame;
_frame.Navigate(source);
}
public void GoBack(object sender, BackPressedEventArgs e)
{
Window.Current.Content = _frame;
if (_frame.CanGoBack)
{
_frame.GoBack();
e.Handled = true;
}
}
}
我背后的 XAML 代码:
public INavigationService NavigationService;
public EmployeeProfilePage()
{
this.InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
...
}
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
NavigationService = new NavigationService();
NavigationService.GoBack(this, e);
}
【问题讨论】:
标签: c# xaml mvvm windows-phone-8.1