Wpf prism-未在shell中加载usercontrol内容的区域
我试图学习prism,但我无法得到一个非常基本的示例。我有一个只有一个区域的壳。我试图将一个视图从模块加载到shell的区域中,但是shell显示一个空白窗口。我无法理解为什么视图没有加载到shell的区域中。下面是代码文件 引导程序:Wpf prism-未在shell中加载usercontrol内容的区域,wpf,prism,prism-4,Wpf,Prism,Prism 4,我试图学习prism,但我无法得到一个非常基本的示例。我有一个只有一个区域的壳。我试图将一个视图从模块加载到shell的区域中,但是shell显示一个空白窗口。我无法理解为什么视图没有加载到shell的区域中。下面是代码文件 引导程序: public class Bootstrapper : MefBootstrapper { protected override DependencyObject CreateShell() { Window shell = new M
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>
要在区域中加载的视图:
<UserControl x:Class="MVVMPractice.Modules.Core.WelcomeView">
<Grid Background="Red">
<TextBlock Text="Hello Prism!" FontSize="20" />
</Grid>
</UserControl>
上面的视图应该显示在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>
虽然我还没有尝试使用app.config配置,但这对我来说是可行的:
public class Bootstrapper : MefBootstrapper
{
#region Overrides of Bootstrapper
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<MainWindow>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (MainWindow) Shell;
Application.Current.MainWindow.Show();
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new ConfigurationModuleCatalog();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (Bootstrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(CoreModule).Assembly));
}
#endregion
}
公共类引导程序:MefBootstrapper
{
#引导程序的区域覆盖
受保护的覆盖依赖对象CreateShell()
{
返回容器.GetExportedValue();
}
受保护的覆盖无效初始值设置Shell()
{
base.InitializeShell();
Application.Current.MainWindow=(MainWindow)Shell;
Application.Current.MainWindow.Show();
}
受保护的覆盖IModuleCatalog CreateModuleCatalog()
{
返回新的ConfigurationModuleCatalog();
}
受保护的覆盖无效配置AggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(Bootstrapper.Assembly));
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(CoreModule.Assembly));
}
#端区
}
//核心模块与您的相同
//主窗口(外壳)
您的Xaml工作正常,但由于您使用了StackPanel,因此该区域无法填充视图。Phil,我编辑了我的问题,以说明我如何使用配置文件配置模块。我认为发现模块应该没问题。此外,我可以看到模块的Initialize()方法正在被调用。唯一的问题是,该区域没有填充我试图添加的视图。谢谢你研究这个问题。Phil,谢谢你尝试代码并在这里发布。从您的代码中可以看出,我在引导程序中缺少了InitializeShell()。有趣的是,窗户还在装东西。这是因为我未能删除App.xaml中的“StartupUri”属性。非常感谢你帮我解决这个问题。菲尔,我已经排除了答案。抱歉耽搁了您为什么不调用RegionManager.RegisterViewWithRegion(“MainRegion”,view),而不是将视图添加到区域中呢。在我看来,它只是将其添加到区域中,而没有显示。我尝试了你的建议,但仍然得到一个空白窗口。这种观点并不以“主要地区”而告终,它应该是这样的。您可以通过在代码中调用RegionManager.Regions[“MainRegion”]来检查这一点,或者通过调试在localwindow中使用它。否则,请尝试为正在加载的ContentControl和usercontrol设置宽度和高度。可能大小不正确视图位于regionManager.Regions[“MainRegion”].ActiveViews集合中,并且是该集合中的唯一视图。我可以从调试器中确认这一点。因此,理想情况下,文本应该出现在屏幕上,但事实并非如此。您的stackpanel(在窗口中)没有拉伸。尝试用网格替换它。或者您可以只设置宽度和高度
public class Bootstrapper : MefBootstrapper
{
#region Overrides of Bootstrapper
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<MainWindow>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (MainWindow) Shell;
Application.Current.MainWindow.Show();
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new ConfigurationModuleCatalog();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (Bootstrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(CoreModule).Assembly));
}
#endregion
}
<ContentControl prism:RegionManager.RegionName="MainRegion" />