问题
In Windows Phone 8.1 there is any way to navigate from one page to another using an string instead of a Type?
There is no reflection and Frame.Navigate only accepts type.
Thank you.
回答1:
I agree with Kai Brummund. You should write a NavigationService. A good example of a navigation service is the MVVM light navigation service. Here you can find the source code.
回答2:
Or... you just can use MVVMLight which comes with an integrated NavigationService (INavigationService). You set it in ViewModelLocator like this:
private const string URL_DETALLE = "/View/DetalleView.xaml";
public ViewModelLocator()
{
// ...
var navigationService = this.CreateNavigationService();
if (!SimpleIoc.Default.IsRegistered<INavigationService>())
{
SimpleIoc.Default.Register<INavigationService>(() => navigationService);
}
//...
}
private INavigationService CreateNavigationService()
{
var navigationService = new NavigationService();
navigationService.Configure(URL_DETALLE, typeof(DetalleView));
return navigationService;
}
Then in your VM constructor you will recieve this navigaton service and you can use it this way:
private INavigationService navigationService;
public DetalleViewModel(INavigationService navigationService)
{
this.navigationService = navigationService;
this.navigationService.NavigateTo(ViewModelLocator.URL_DETALLE);
}
Hope it helps ;)
来源:https://stackoverflow.com/questions/28157188/windows-phone-8-1-navigate-using-string-instead-of-a-type