我最近刚刚建立了一个新项目,使用 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));
}
}
主外壳:
<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 未设置。
我该如何调试这个?我可能做错了什么?