Get Navigation data in View Model Windows phone 8

家住魔仙堡 提交于 2020-01-07 04:23:39

问题


I'm using Mvvmlight INavigationService in the project, from view model I navigate to a view , and pass object as parameter ,how do I get either in the view model or in the code behind.

I have implemented based on this

My View model locator

private static INavigationService CreateNavigationService()
    {
        var navigationService = new NavigationService();

        navigationService.Configure("topupdetails", new System.Uri("/Views/TopUpDetails.xaml", System.UriKind.Relative));

        return navigationService;
    }

View Model command executes

private void OnTapCommand(MyModel tappedItem)
    {
        if (tappedItem._verified)
        {
        SimpleIoc.Default.GetInstance<INavigationService>().NavigateTo("topupdetails",tappedItem);

        }

Page I navigated to

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (NavigationContext.QueryString.Any())
        {
            var item = NavigationContext.QueryString.Values.First();

        }
        base.OnNavigatedTo(e);
    }

In query string I just value like this "e-fghfdsfsd" but cant cast to my class object , Can I get the value in view model or in the code-behind which is best way to do this , with a minimum code in code-behind ?

Mvvmlight Version: Mvvmlight 5 Laurent Mvvmlight 5 Announcement

I have also referred this


回答1:


You can use the NavigationService to evaluate the NavigationContext:

GalaSoft.MvvmLight.Views.NavigationService _navigationService = [..] // Get your NS instance here or create a new one.
var m = _navigationService.GetAndRemoveParameter(NavigationContext);
// Try to cast:
MyModel model = m as MyModel;
// Deny if it's not a valid object
if (model == null)
    return;


来源:https://stackoverflow.com/questions/28615841/get-navigation-data-in-view-model-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!