1

I just set up a new project recently, using Prism 6.1(with Unity).

I've my bootstrapper:

public class ServerBootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return Container.Resolve<MainShell>();
    }
    protected override void InitializeShell()
    {
        base.InitializeShell();
        Application.Current.MainWindow = (Window)Shell;
        Application.Current.MainWindow.Show();
    }
    protected override void ConfigureModuleCatalog()
    {
        base.ConfigureModuleCatalog();
        ModuleCatalog catalog = (ModuleCatalog)ModuleCatalog;
        catalog.AddModule(typeof(ServerUIModule));
    }
}

The MainShell:

<Window.Resources>
    <Style TargetType="TabItem">
        <Setter Property="Header" Value="{Binding DataContext.Title}" />
    </Style>
</Window.Resources>
<DockPanel LastChildFill="True">
    <TabControl regions:RegionManager.RegionName="{x:Static infrastructure:RegionsNames.MAIN_TAB_REGION}" />
</DockPanel>

My Module definition:

public class ServerUIModule : IModule
{
    private readonly IRegionManager m_regionManager;

    public ServerUIModule( IRegionManager regionManager)
    {
        m_container = container;
        m_regionManager = regionManager;
    }

    public void Initialize()
    {
        m_regionManager.RegisterViewWithRegion(RegionsNames.MAIN_TAB_REGION, typeof(StatusView));
        m_regionManager.RegisterViewWithRegion(RegionsNames.MAIN_TAB_REGION, typeof(LogMessagesView));
    }
}

My StatusView:

<UserControl x:Class="Xms.Server.UI.Views.StatusView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Xms.Server.UI.Views"
             xmlns:mvvm="http://prismlibrary.com/"
             xmlns:statusControl="clr-namespace:Xms.Server.UI.Views.StatusControl"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="Aquamarine" mvvm:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <TextBlock>Content</TextBlock>
    </Grid>
</UserControl>

The corresponding ViewModel:

public class StatusViewModel : BindableBase
{
    public String Title => LocalizedResources.StatusModuleTitle;

    public StatusViewModel()
    {
        //This constructor is never called
    }
}

The issue is that my constructor is never called, my DataContext is not set.

How can I debug this? What could I've done wrong?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • StatusView is created but not StatusViewModel? – Haukinger Mar 04 '16 at 09:34
  • @Haukinger Exactly, but with the `ViewModelLocator.AutoWireViewModel="True"` It should find the ViewModel matching the View(6.1 convention is to add "Model" to the "XxxxView" to search the class). But I can't find any log indicating it failed to find it, ... – J4N Mar 04 '16 at 09:38

1 Answers1

4

First, make sure that the views reside in a Views namespace, and the viewmodels in ViewModels, respectively.

Second, the easiest way of debugging this type of things is copying some code from the prism sources and put this in MyBootstrapper.ConfigureContainer:

ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver( x =>
                                                                     {
                                                                         var viewName = x.FullName;
                                                                         viewName = viewName.Replace( ".Views.", ".ViewModels." );
                                                                         var viewAssemblyName = x.GetTypeInfo().Assembly.FullName;
                                                                         var suffix = viewName.EndsWith( "View" ) ? "Model" : "ViewModel";
                                                                         var viewModelName = string.Format( CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName );
                                                                         return Type.GetType( viewModelName );
                                                                     } );
ViewModelLocationProvider.SetDefaultViewModelFactory( type => Container.Resolve( type ) );

...and put breakpoints there to see exactly what's the problem. Alternatively, dig into your XamlParseException's InnerExceptions... at about the third level you should find the real problem. But I find my breakpoint-approach more convenient.

Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • Views/ViewsModels namespace is wrong. This was correct with Prism 5, but according to their release note, I understood their changed their default naming convention? I will test your Code(but I guess it should be placed in `ConfigureViewModelLocator`, not `ConfigureContainer` – J4N Mar 04 '16 at 09:57
  • Well, it seems I misunderstood the change of convention(Brian Lagunas was recommanding to change the convention, and since he is the new owner, I thought it was following his own recommandation. Anyway, I changed the convention myself, everything is working now. – J4N Mar 04 '16 at 10:02