Prism 6.1 ViewModelLocator不实例化我的ViewModel

时间:2016-03-04 09:17:24

标签: c# .net wpf prism viewmodellocator

我最近使用Prism 6.1(使用Unity)设置了一个新项目。

我是我的引导程序:

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>

我的模块定义:

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));
    }
}

我的状态视图:

<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>

相应的ViewModel:

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

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

问题是我的构造函数永远不会被调用,我的DataContext没有被设置。

我该如何调试?我做错了什么?

1 个答案:

答案 0 :(得分:4)

首先,确保视图分别驻留在Views命名空间中,并且视图模型分别位于ViewModels中。

其次,调试此类事情的最简单方法是从棱镜源复制一些代码并将其放在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 ) );

...并在那里放置断点以确切了解问题所在。或者,深入了解XamlParseException InnerException ... ...在第三级,您应该找到真正的问题。但我发现我的断点方法更方便。