【发布时间】:2012-03-19 06:41:17
【问题描述】:
我试图学习 prism,但我无法获得一个非常基本的示例。我有一个外壳,它有一个区域。我正在尝试将模块中的视图加载到外壳的区域中,但外壳显示一个空白窗口。我只是无法弄清楚为什么视图没有加载到 shell 的区域中。这是代码文件。
引导程序:
public class Bootstrapper : MefBootstrapper {
protected override DependencyObject CreateShell() {
Window shell = new MainWindow();
Application.Current.MainWindow = shell;
return shell;
}
protected override IModuleCatalog CreateModuleCatalog() {
return new ConfigurationModuleCatalog();
}
}
壳牌:
<Window x:Class="MVVMPractice.MainWindow">
<StackPanel x:Name="LayoutRoot" Background="White">
<ContentControl prism:RegionManager.RegionName="MainRegion" />
</StackPanel>
</Window>
模块:
[ModuleExport(typeof(CoreModule))]
public class CoreModule : IModule {
[Import]
public IRegionManager RegionManager { get; set; }
public void Initialize() {
var view = new WelcomeView();
RegionManager.AddToRegion("MainRegion", view);
}
}
要在区域中加载的视图:
<UserControl x:Class="MVVMPractice.Modules.Core.WelcomeView">
<Grid Background="Red">
<TextBlock Text="Hello Prism!" FontSize="20" />
</Grid>
</UserControl>
上面的视图应该显示在 shell 中,但它没有。任何想法为什么 shell 的区域没有显示上述视图?
编辑: 模块通过 App.config 文件进行配置,如下所示。
App.config
<configuration>
<configSections>
<section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection,
Microsoft.Practices.Prism"/>
</configSections>
<modules>
<module assemblyFile="MVVMPractice.Modules.Core.dll"
moduleType="MVVMPractice.Modules.Core.CoreModule, MVVMPractice.Modules.Core.CoreModule,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
moduleName="CoreModule"
startupLoaded="true" />
</modules>
</configuration>
【问题讨论】:
-
你为什么不打电话给
RegionManager.RegisterViewWithRegion("MainRegion", view),而不是将视图添加到该区域。在我看来,它只是将它添加到该区域而不显示它。 -
我尝试了你的建议,但我仍然得到一个空白窗口。视图不知何故不会出现在“MainRegion”中。
-
应该。您可以通过在代码中调用 RegionManager.Regions["MainRegion"] 来检查这一点,或者通过调试在本地窗口中使用它。否则尝试为您正在加载的 ContentControl 和用户控件设置宽度和高度。可能是尺寸不合适
-
视图位于 regionManager.Regions["MainRegion"].ActiveViews 集合中,并且是该集合中的唯一视图。我可以从调试器中确认。所以理想情况下,文本应该出现在屏幕上,但实际上并没有。
-
就像我说的,这一定是样式问题。您的堆栈面板(在窗口中)没有拉伸。尝试用网格替换它。或者你可以只设置宽度和高度