Reputation: 6786
I have this hyperlink:
<Hyperlink NavigateUri="Page2.xaml?dummy=Kirk">Go to page 2</Hyperlink>
And then this parsing code in the linked page Page2.xaml.cs
:
public string GetQueryStringParameter(string key)
{
var spl = NavigationService.Source.Query.Split(',');
foreach (var s in spl)
{
var spl2 = s.Split('=');
if (spl2[0] == key)
return spl2[1];
}
throw new ArgumentException($"Could not find parameter {key} in query string {NavigationService.Source.Query}.");
}
public string Dummy => GetQueryStringParameter("dummy");
However GetQueryStringParameter
crashes trying to retrieve the NavigationService
, which is null for some reason. Why is the navigation service null? How else can I get the query string?
Upvotes: 0
Views: 123
Reputation: 26075
I think your View/ViewModel is trying to access Dummy
property while object is still not initialized, meaning someone is accessing NavigationService
when it is not yet set by the framework. Make sure your code lets Page to initialize before accessing NavigationService
property.
For example, if you are using Dummy
property in view model, you can set viewmodel after your view is loaded:
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.Loaded += MainWindow_Loaded;
DataContext = new MainViewModel();
}
Upvotes: 1