无法导航回Template10

时间:2016-04-15 15:28:06

标签: c# xaml navigation template10

我正在尝试从此辅助页面导航回我的MainPage。我尝试使用NavigationService.GoBack(),但我得到NullReferenceException

我没有从viewmodel更改任何内容。我打算做的是先将用户输入保存到SQLite中,然后导航回MainPage

以下是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);
    }

以下是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();
    }

附注:我尝试使用GotoMainPageDetail.xaml.cs访问vm.GotoMainPage(),但仍然返回异常

1 个答案:

答案 0 :(得分:0)

  • 要在不同的页面之间导航,请使用Frame.Navigate方法。
  • 页面导航到名为Mainpage的xaml页面的示例是: this.Frame.Navigage(typeof(Mainpage));

有关详细信息,请查看文档:{​​{3}}

正在使用的命名空间称为System.Windows.Controls