【发布时间】:2016-10-14 20:12:18
【问题描述】:
我正在使用 MVVM 开发 Windows Phone 8.1 应用程序。
我有包含如下导航服务的基本视图模型类:
public abstract class BaseViewModel : INotifyPropertyChanged
{
protected readonly INavigationService NavigationService;
//....
}
还有我的导航服务类:
public class NavigationService : INavigationService
{
public void Navigate(Type destinationPage)
{
((Frame)Window.Current.Content).Navigate(destinationPage);
}
public void Navigate(Type desitnationPage, object parameter)
{
((Frame)Window.Current.Content).Navigate(desitnationPage, parameter);
}
public void GoBack()
{
((Frame)Window.Current.Content).GoBack();
}
}
当我从 XAML 绑定命令时,一切正常。当我想覆盖BackButton 时出现问题。我还创建了包含NavigationService 的基本页面模型。每个页面都有一个覆盖 pf BackPressed 如下:
public class BasePage : Page
{
protected INavigationService NavigationService => ComponentManager.GetInstance<INavigationService>();
public BasePage()
{
//...
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
protected virtual void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
//Frame.Navigate(typeof(MainPage));
(this.DataContext as BaseViewModel)?.Back.Execute(sender);
}
}
正如您在HardwareButtons_BackPressed 方法中看到的那样,我已经尝试过使用方法,但没有一种方法有效。每次我按下返回按钮时应用程序都会崩溃而没有任何错误。
【问题讨论】:
标签: c# mvvm windows-phone-8.1