Windows 8.1 Store App MainPageViewModel not autowired

Oct 15, 2014 at 2:19 PM
Edited Oct 15, 2014 at 3:09 PM
Hi,

I'm developing my first Windows 8.1 Store App with Prism and looked at a bunch of tutorials and got a few things working. But what I have trouble with, is autowiring the ViewModel to the MainPage.
I got other pages working, so it's not that it doesn't work at all.
It seems that I do something wrong with navigation from App.xaml.cs to MainPage.

In MainPage.xaml I got this configuration:
<prismStorapps:VisualStateAwarePage
    x:Class="MyApp.Views.MainPage"
    prism:ViewModelLocator.AutoWireViewModel="true" ...
In app.xml.cs I got the navigation to MainPage:
    sealed partial class App : MvvmAppBase
    {
        private readonly IUnityContainer _container = new UnityContainer();

        public App()
        {
            this.InitializeComponent();
        }

        protected override System.Threading.Tasks.Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
        {
            this.NavigationService.Navigate("Main", null);
            return (Task.FromResult<object>(null));
        } ...
It navigates to the page OK, but it doesn't autowire the ViewModel.

What am I doing wrong?

Thanks in advance!
Rico
Oct 16, 2014 at 9:59 PM
Hi,

1st) Make sure your naming convention is correct: MainPage - MainPageViewModel, 2nd) do you have the override define in your App.xaml.cs:
        protected override object Resolve(Type type)
        {
            // Use the container to resolve types (e.g. ViewModels and Flyouts)
            // so their dependencies get injected
            return _container.Resolve(type);
        }
and make sure your read the rest of this chapter starting from "Using a dependency injection container" because I'm using Unity to resolve my ViewModels and if your not this well show you how.
Marked as answer by schlumpfling42 on 10/16/2014 at 2:38 PM
Oct 16, 2014 at 10:10 PM
Hi,

1) Double checked that, it's named correctly.
2) I did that too, just forgot to mention it.
2a) Resolve is called when it navigates from the MainPage to another Page. It seems that for the MainPage a completely different mechanism is used. I would think, that something is not initialized yet, but I cannot figure out what's missing.

Regards,
Rico
Oct 16, 2014 at 10:38 PM
Hi,

darn ... I found the Problem. It was the Namespace, in this one ViewModel class I called it .View not .Views.
Sorry about the trouble, but thank you for looking into it anyway.

Regards,
Rico
Oct 16, 2014 at 11:30 PM
Good, glad you found the problem! :) I was also going to suggest to make sure you have registered with your container:
_container.RegisterType<NathsarTS.UILogic.ViewModels.MainPageViewModel>(new ContainerControlledLifetimeManager());