代码之家  ›  专栏  ›  技术社区  ›  Sonny D

在Shell.xaml中创建区域期间发生KeyNotFoundException

  •  0
  • Sonny D  · 技术社区  · 7 年前

    我刚从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未在区域适配器中注册 映射。您可以通过以下方式为此控件注册iRegionadAdapter 重写中的ConfigureRegionAdapterMappings方法 引导程序。

    好吧,但是 AutofacBootstrapper 类没有任何名为 ConfigureRegionAdapterMappings 去超越。

    首先我觉得有点不对劲 自动引导程序 ,但即使我把它改成 UnityBootstrapper 问题依然存在。但是第二个允许我重写 配置RegionAdapterMappings

    1 回复  |  直到 7 年前
        1
  •  2
  •   Haukinger    7 年前

    一个 Grid 不是区域的有用宿主。尝试使用 ContentControl 相反。

    当然,如果您绝对想使用 网格 ,但我真的看不到任何好处。