简体   繁体   English

无法在Template10中导航

[英]Cannot navigate back in Template10

I'm trying to navigate back to my MainPage from this secondary page. 我正在尝试从此辅助页面导航回我的MainPage。 I tried to use NavigationService.GoBack() but I got NullReferenceException . 我尝试使用NavigationService.GoBack()但得到了NullReferenceException

I didn't change anything from the viewmodel. 我没有从视图模型中进行任何更改。 What I intended to do is to save user input into SQLite first, then navigate back to MainPage 我打算做的是先将用户输入保存到SQLite,然后导航回MainPage

Here is my code from DetailPage.xaml.cs 这是我来自DetailPage.xaml.cs代码

    private SQLiteService database = new SQLiteService();
    DetailPageViewModel vm = new DetailPageViewModel();

    public DetailPage()
    {
        InitializeComponent();
        NavigationCacheMode = NavigationCacheMode.Disabled;
    }

    private void yesButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {            
        var _name = Name.Text;
        var _uptake = UptakeTime.SelectedIndex + 1; // database index Morning start at 1
        var _intake = int.Parse(Intake.Text);

        vm.ProcessData(_name, _intake, _uptake);
    }

Here is the DetailPageViewModel.cs 这是DetailPageViewModel.cs

    SQLiteService database = new SQLiteService();

    public DetailPageViewModel()
    {
        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            Value = "Designtime value";
        }
    }

    private string _Value = "Default";
    public string Value { get { return _Value; } set { Set(ref _Value, value); } }

    public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
    {
        Value = (state.ContainsKey(nameof(Value))) ? state[nameof(Value)]?.ToString() : parameter?.ToString();
        await Task.CompletedTask;
    }

    public override async Task OnNavigatedFromAsync(IDictionary<string, object> pageState, bool suspending)
    {
        if (suspending)
        {
            pageState[nameof(Value)] = Value;
        }
        await Task.CompletedTask;
    }

    public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
    {
        args.Cancel = false;
        await Task.CompletedTask;
    }

    public void GotoMainPage() =>
        NavigationService.GoBack();    

    public void ProcessData(string _name, int _type, int _uptake)
    {
        database.AddNewItem(_name, _uptake, _type);
        GotoMainPage();
    }

Side note : I tried to access the GotoMainPage from Detail.xaml.cs by using vm.GotoMainPage() , but it still returned exception 旁注:我尝试使用vm.GotoMainPage()Detail.xaml.cs访问GotoMainPage ,但它仍返回异常

  • To navigate between different Pages use the Frame.Navigate method. 要在不同的页面之间导航,请使用Frame.Navigate方法。
  • An example for page Navigation to a xaml Page called Mainpage is: this.Frame.Navigage(typeof(Mainpage)); 页面导航到名为Mainpage的xaml页面的示例是: this.Frame.Navigage(typeof(Mainpage));

For further Information look at the Documentation: Frame.Navigate 有关更多信息,请参见文档: Frame.Navigate

The Namespace being used is called System.Windows.Controls . 使用的命名空间称为System.Windows.Controls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM