【问题标题】:KeyNotFoundException during create region in Shell.xaml在 Shell.xaml 中创建区域期间出现 KeyNotFoundException
【发布时间】:2018-07-27 15:31:52
【问题描述】:

我刚开始使用 PRISM,遇到了一个我无法解决的异常。

<Window x:Class="Workplace.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="http://www.codeplex.com/prism"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid Name="Header" prism:RegionManager.RegionName="Header">

        </Grid>
    </Grid>
</Window>

.

using Autofac;
using Prism.Autofac;
using Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Workplace
{
    class Bootstrapper : AutofacBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return new Shell();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            Application.Current.MainWindow = (Window) this.Shell;
            Application.Current.MainWindow.Show();
        }

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
        }
    }
}

运行后出现异常:

KeyNotFoundException:该类型的 IRegionAdapter System.Windows.Controls.Grid 未在区域适配器中注册 映射。您可以通过以下方式为此控件注册一个 IRegionAdapter 覆盖中的 ConfigureRegionAdapterMappings 方法 引导程序。

好的,但是 AutofacBootstrapper 类没有任何名为 ConfigureRegionAdapterMappings 的方法可以覆盖。

首先我认为AutofacBootstrapper 有问题,但即使我将其更改为UnityBootstrapper,问题仍然存在。但是第二个允许我覆盖ConfigureRegionAdapterMappings

【问题讨论】:

    标签: wpf prism


    【解决方案1】:

    Grid 不是区域的有用主机。尝试改用ContentControl

    当然,如果你绝对想使用Grid,你可以创建并注册一个自定义区域适配器,但我看不出有什么好处。

    【讨论】:

    • Okey 问题已解决。现在对我来说很明显为什么Grid 不足以定义区域,它不继承自ContentControl。艰难的开始,非常感谢。 :D
    • 现在我明白了,PRISM 有内置的适配器(ContentControlRegionAdapter、SelectorRegionAdapter、ItemsControlRegionAdapter),可以将一些Controls 调整为一个区域,更多:prismlibrary.github.io/docs/wpf/…。就像您提到的那样,我可以编写自己的 adpater,但这毫无意义。
    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 2022-07-25
    相关资源
    最近更新 更多