【问题标题】:prism - region not loading contents of usercontrol in shellprism - 区域未在 shell 中加载用户控件的内容
【发布时间】: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 集合中,并且是该集合中的唯一视图。我可以从调试器中确认。所以理想情况下,文本应该出现在屏幕上,但实际上并没有。
  • 就像我说的,这一定是样式问题。您的堆栈面板(在窗口中)没有拉伸。尝试用网格替换它。或者你可以只设置宽度和高度

标签: wpf prism prism-4


【解决方案1】:

这对我有用,虽然我还没有尝试使用 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
}

//核心模块和你的一样

// 主窗口(Shell)

<ContentControl prism:RegionManager.RegionName="MainRegion"  />

您的 Xaml 有效,但由于您使用了 StackPanel,因此该区域不会填满视图。

【讨论】:

  • Phil,我已经编辑了我的问题,以表明我如何使用配置文件配置模块。我认为对于要发现的模块应该没问题。此外,我可以看到我的 Module 的 Initialize() 方法正在被调用。唯一的问题是该区域没有填充我要添加的视图。感谢您调查这个问题。
  • Phil,感谢您尝试代码并在此处发布。我可以从您的代码中看到我的 Bootstrapper 中缺少 InitializeShell()。有趣的部分是窗口仍在加载。那是因为我未能删除 App.xaml 中的“StartupUri”属性。非常感谢您帮助我解决问题。
  • 菲尔,我已经排除了答案。抱歉耽搁了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
相关资源
最近更新 更多