Reputation: 4765
I can't seem to use traditional navigation in Template10. I always get Navigation failed errors. Does Template10 require that I use 'XAML behavior style' navigation instead of my code behind navigation like before?
I am also capturing the inner exception and these are the errors I see there:
Unexpected character encountered while parsing value: h. Path '', line 0, position 0. Your parameter must be serializable. If it isn't, then use SessionState.
(but my parameter is just a string)
private void lvResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
int intIndex = lvResults.SelectedIndex;
string strShowLink = g_ro.webPages.value[intIndex].displayUrl;
//This is what is returned in the line above and I want it as my passed parameter :
//https://www.grc.com/sn/sn-482.htm
//Open Detailspage sending parameter as a string
Frame.Navigate(typeof(BlankPage1),strShowLink);
}
catch (Exception ex)
{
//Error caught in app.xaml.cs (UnhandleExceptio)
//Windows.UI.Xaml.Controls.Frame.NavigationFailed was unhandled.
string strEx = ex.Message;
}
Upvotes: 1
Views: 1633
Reputation: 10841
I made a basic demo and reproduced your problem. I looked into template 10 source code and found that template 10 added an event on frame.Navigating like below:
frame.Navigating += (s, e) => FacadeNavigatingCancelEventHandler(s, e);
private async void FacadeNavigatingCancelEventHandler(object sender, NavigatingCancelEventArgs e)
{
...
object parameter = null;
try
{
parameter = SerializationService.Deserialize(e.Parameter?.ToString());
}
catch (Exception ex)
{
throw new Exception("Your parameter must be serializable. If it isn't, then use SessionState.", ex);
}
...
}
So it will try to deserialize your parameter when navigating. You have to serialize your parameter first like below:
using Template10.Services.SerializationService;
...
string param = @"https://www.grc.com/sn/sn-482.htm";
string str=SerializationService.Json.Serialize(param);
Frame.Navigate(typeof(OtherPage), str);
And the error will be gone. But if you simply want to navigate on code-behind, you can also use NavigationService like below:
string param = @"https://www.grc.com/sn/sn-482.htm";
var NavService = NavigationService.GetForFrame(Frame);
NavService.Navigate(typeof(OtherPage), param);
Upvotes: 3
Reputation: 3924
There is the premise that you are using MVVM practices and have a viewmodel set to the datacontext (View-First) in the page in question. That viewmodel will inherit ViewModelBase which indeed does have NavigationService property by default. Otherwise you would inherit INavigable build your own viewmodel and setup the items that are "required" by the INavigable interface.
This also introduces the separation of concerns from the view to be responsible only for "showing" not doing (unless it is view related) and processing the "Clicks, Selections, etc" on the viewmodel
Keep in mind that this also pushes you to either bind properties by defined type x:Bind
or late binding "{Binding SomeProperty}"
Upvotes: 0