我正试图从这个辅助页面导航回我的MainPage。我试着使用NavigationService.GoBack()
,但我得到了NullReferenceException
。
我没有更改视图模型中的任何内容。我打算做的是先将用户输入保存到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();
}
附带说明:我尝试使用vm.GotoMainPage()
从Detail.xaml.cs
访问GotoMainPage
,但它仍然返回异常
- 要在不同页面之间导航,请使用
Frame.Navigate
方法 - 页面导航到名为Mainpage的xaml页面的示例如下:
this.Frame.Navigage(typeof(Mainpage));
有关更多信息,请参阅文档:框架。导航
正在使用的命名空间称为System.Windows.Controls
。