How to auto-wire QueryString to VM property on WP7

Sep 22, 2010 at 3:06 AM

Hi

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?

Cheers

Steve

Coordinator
Sep 22, 2010 at 1:11 PM

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

Sep 22, 2010 at 8: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
        {
            get
            {
                return _items;
            }
        }

        public int UserId { get; set; }

        public JobTypeViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;

            KernServiceClient client = new KernServiceClient();

            client.GetJobTypesCompleted += client_GetJobTypesCompleted;

            client.GetJobTypesAsync(UserId);

        }

        void client_GetJobTypesCompleted(object sender, GetJobTypesCompletedEventArgs e)
        {
            foreach (var jobTypeDto in e.Result)
            {
                _items.Add(jobTypeDto);
            }
        }

    }

Cheers

 

Steve

 

 

Sep 22, 2010 at 9: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.

-Bryan

Sep 22, 2010 at 10:03 PM

Thanks Bryan  - works great.

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

Cheers

Steve

Coordinator
Sep 22, 2010 at 11:58 PM

The closest thing I have to that right now is this: http://caliburnmicro.codeplex.com/wikipage?title=Working%20with%20Windows%20Phone%207&referringTitle=Documentation

Sep 23, 2010 at 12: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.

Cheers

Steve