cimbalino / Cimbalino-Phone-Toolkit

Cimbalino Windows Phone Toolkit
MIT License
78 stars 32 forks source link

Navigation Service Query Parameter Problem #37

Closed chobo2 closed 10 years ago

chobo2 commented 10 years ago

Hi I am not sure if this is an issue or limitation or what it is. I wanted to to grab a parameter while in the constructor but I don't seem to be able to do this.

public class MainViewModel : ViewModelBase
{

    private readonly INavigationService navigationService = null;
    public MainViewModel(INavigationService navigationService)
    {
        this.navigationService = navigationService;
        NavigateToPg2WithParmsCmd = new RelayCommand(() => NaviagateToPg2WithParms()); 
    }

    private void NaviagateToPg2WithParms()
    {

        navigationService.NavigateTo(new Uri("/Views/SecondPg.xaml?parameter=1&parm2=2", UriKind.Relative));
    }

    public RelayCommand NavigateToPg2Cmd
    {
        get;
        private set;
    }

    public RelayCommand NavigateToPg2WithParmsCmd
    {
        get;
        private set;

}       

    private readonly INavigationService navigationService = null;
    public SecondVM(INavigationService navigationService)
    {
        this.navigationService = navigationService;

        if (IsInDesignMode)
        {
            Message = "Design Mode";
        }
        else
        {
            if (navigationService.QueryString.ContainsKey("paramter"))
            {
                 Message = navigationService.QueryString["parameter"];
            }

        }

but it is always blank. Am I doing something wrong?

rikkit commented 10 years ago
 if (navigationService.QueryString.ContainsKey("paramter"))
            {
                 Message = navigationService.QueryString["parameter"];

Spelling mistake?

chobo2 commented 10 years ago

Opps forgot to change that. I saw that as writing the OP. I fixed that but still not the case as the navigationService still has a count of zero in the query string.

pedrolamas commented 10 years ago

I don't think you'll be able to access the NavigationContext from the constructor as it is not set at that time... my advice is that you just Dispatcher.BeginInvoke() on the constructor code, and access the QueryString there!

chobo2 commented 10 years ago

Ok I will try that, Will that not break the MVVM pattern though? My plan was to use the querystring to pass the id of an item I need for that view and then pull that item out of app storage. Otherwise I am not sure the proper way to send data between views is for the phone(using MVVM Light).

pedrolamas commented 10 years ago

If you are using MVVMLight, then you should use the DispatcherHelper as that is the proper way of handling this while not breaking the MVVM pattern!

chobo2 commented 10 years ago

Hey Maybe I am not understanding something but I tried

    public SecondVM(INavigationService navigationService)
    {
        this.navigationService = navigationService;

        if (IsInDesignMode)
        {
            Message = "Design Mode";
        }
        else
        {

            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                if (navigationService.QueryString.ContainsKey("parameter"))
                {
                    Message = navigationService.QueryString["parameter"];
                }
            });              
        }          
    }

and still did not work for me.

pedrolamas commented 10 years ago

You can't use DispatcherHelper.CheckBeginInvokeOnUI() because given the fact you are running on the UI thread, it will just run the code immediately instead of dispatching it. Use DispatcherHelper.RunAsync() instead and everything should work ok!

chobo2 commented 10 years ago

So the View Model is running on the UI already? Changing it to RunAsync worked! Now I have to figure out what is better way to send data between View is.

  1. Is it pass an id through the parameter then doing a lookup?
  2. Create basically a model and hook that up through SimpleIoc and every time I need data pass in that Model to each ViewModel.

Those are the 2 ways I found so far.