Prism 6.1 ViewModelLocator没有实例化我的ViewModel [英] Prism 6.1 ViewModelLocator doesn't instantiate my ViewModel

查看:331
本文介绍了Prism 6.1 ViewModelLocator没有实例化我的ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚刚使用Prism 6.1(带有Unity)建立了一个新项目.

I just set up a new project recently, using Prism 6.1(with 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));
    }
}

MainShell:

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:

The corresponding ViewModel:

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

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

问题是我的构造函数从未被调用,我的DataContext未设置.

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?

推荐答案

首先,确保视图位于Views命名空间中,视图模型分别位于ViewModels中.

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

第二,调试此类事物的最简单方法是从棱镜源复制一些代码并将其放入MyBootstrapper.ConfigureContainer:

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

...并在此处放置断点以确切地了解问题所在.或者,在第3层中深入研究XamlParseExceptionInnerException ...,您应该会发现真正的问题.但是我发现我的断点方法更方便.

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

这篇关于Prism 6.1 ViewModelLocator没有实例化我的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆