How to auto-wire QueryString to VM property on WP7

Sep 22, 2010 at 4:06 AM


I am trying to auto-wire up a query string I am passing from one page to a property on the view model of my second page.

I have created a property with the same name as the query string I am passing but it not taking the value.

I am using soup to nuts part 4 as an example. Unfortunately the vm used as an example there has lots of conductors and tabs and I can't see which bits I need just for the query string handling.

I'm injecting a navigation service into the constructor of my second vm. Anythign else I need to do? Do I need to implement an interface?



Sep 22, 2010 at 2:11 PM

Is your property public? Can you post some code here?

Sep 22, 2010 at 9:07 PM

This passes in the parameter:

    string targetUri = string.Format("/Views/JobType.xaml?UserId={0}", userId);
    _navigationService.Navigate(new Uri(targetUri, UriKind.RelativeOrAbsolute));

This is the target view model:

    public class JobTypeViewModel
        readonly INavigationService _navigationService;
        private readonly ObservableCollection<JobTypeDto> _items = new ObservableCollection<JobTypeDto>();

        public ObservableCollection<JobTypeDto> Items
                return _items;

        public int UserId { get; set; }

        public JobTypeViewModel(INavigationService navigationService)
            _navigationService = navigationService;

            KernServiceClient client = new KernServiceClient();

            client.GetJobTypesCompleted += client_GetJobTypesCompleted;



        void client_GetJobTypesCompleted(object sender, GetJobTypesCompletedEventArgs e)
            foreach (var jobTypeDto in e.Result)







Sep 22, 2010 at 10:41 PM

Hey Steve, you're trying to grab the UserId too soon. 

Try this:
- Make JobTypeViewModel derive from Screen. 
- Add a protected override void OnInitialize(){ ... }
- Move your  "client.GetJobTypesAsync(UserId);" into the OnInitialize
- Put a breakpoint in your constructor and one in OnInitialize
- Debug and see when UserId is set.

Take a look at FrameAdapter::OnNavigated to see how the QueryString gets parsed and injected.


Sep 22, 2010 at 11:03 PM

Thanks Bryan  - works great.

This sort of info would be great in a FAQ or wiki.



Sep 23, 2010 at 12:58 AM

The closest thing I have to that right now is this:

Sep 23, 2010 at 1:31 AM

Thanks Rob. That article certainly got me up and running with using Caliburn Micro on WP7. I think it may also be useful to have a FAQ with simple questions and answers like:

Q: How do I wire up a button event handler

A: Create a method in the View Model with the same name as the button.

Q: How do I enable/disable a button

A: Create a public property in the event handler called Can<button_name>

Q: What should my View Model inherit from and why

A: Screen...because...

That sort of thing.