Question

Hello I want to pass a string to another viewmodel. I am trying to use this code but it does not serve my purpose as I need to know the string value in the recipient views' viewmodel. How can I accomplish this? You can also suggest better ways of doing it with mvvm-light and commands because my concept is not that much clear with these two.

 public MainViewModel()
 {
        GiveDetails = new RelayCommand<object>(DoSomething);
 }

 private void DoSomething(object param)
 {
        var rootFrame = (App.Current as App).RootFrame;
        rootFrame.Navigate(new System.Uri("/Details.xaml?ID="+param.ToString(), System.UriKind.Relative));
 }

I currently dont have a clue how to process the parameter on the recipient viewmodel.

Was it helpful?

Solution

In Stack Careers Browser when a career is selected from the main page view, I have the following in the OnNavigatedTo event on the DetailsPage.xaml.cs.

  if (DataContext == null)
        {
            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                **int index = int.Parse(selectedIndex);**
                **DataContext = _vm = new JobPostingViewModel(App.ViewModel.JobPostings[index]);**
                Indicators.SetIndicators(this, DataContext);
                await _vm.ScrapeThatScreenAsync(_vm.JobPosting.Id);
            }
        }

The corresponding viewmodel has a constructor that looks like this:

public JobPostingViewModel(JobPosting jobPosting)
    {
        if (jobPosting == null)
            throw new ArgumentNullException("jobPosting");

        JoelTestResults = new ObservableCollection<JoelTestResult>();
        // JoelTestResults = new ObservableCollection<string>(){"a","b","c"};
        _jobPosting = jobPosting;
        if (jobPosting.Categories != null && jobPosting.Categories.Any())
            ProcessCategories(jobPosting.Categories);
    }

Where I require some basic object in order to process more information and display it in the windows phone 8 manner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top