Windows Phone 8.1 navigate using string instead of a type
Asked Answered
P

2

0

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.

Prostatitis answered 26/1, 2015 at 19:20 Comment(5)
Most people write a NavigationService that wraps the frame navigation an looks up page names in a dictionary. You will still have to register the types somewhere.Bencion
What do you mean, there is no reflection?Septuplet
any problem with types?Orvilleorwell
@KaiBrummund Nice idea, the best is do a dictionary! :-)Prostatitis
@BlackCid Having a NavigationService ot some sort is generally a good idea, and you should go for it. I'm pretty sure you can also use Reflection, though, if you need to.Septuplet
T
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.

Thoroughfare answered 27/1, 2015 at 11:29 Comment(0)
S
0

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 ;)

Synonymous answered 27/2, 2015 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.