在xaml

时间:2017-10-18 16:30:38

标签: c# wpf xaml mef

我正在专门针对https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/6-ViewActivationDeactivation查看PrismLibrary的WPF样本。我的目标是创建ViewActivationDeactivation解决方案的工作版本,但使用MefBootstrapper而不是最初在示例中使用的UnityBoostrapper。

我已经像这样配置了我的引导程序类

public class Bootstrapper : MefBootstrapper
{
    protected override void ConfigureAggregateCatalog()
    {
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        base.ConfigureAggregateCatalog();
    }
    protected override DependencyObject CreateShell()
    {
        return Container.GetExportedValue<MainWindow>();
    }

    protected override void InitializeShell()
    {
        Application.Current.MainWindow = (MainWindow)Shell;
        Application.Current.MainWindow.Show();
    }

    protected override CompositionContainer CreateContainer()
    {
        var container = base.CreateContainer();
        container.ComposeExportedValue(container);
        return container;
    }
}

并且MainWindow的代码如下所示:

xaml

<Window x:Class="DuoApp_ViewNav.MainWindow"
        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:local="clr-namespace:DuoApp_ViewNav"
        xmlns:v="clr-namespace:DuoApp_ViewNav.Views"
             xmlns:prism="http://prismlibrary.com/"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel LastChildFill="True">
        <ContentControl prism:RegionManager.RegionName="ContentRegion" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </DockPanel>
</Window>

xaml.cs

[Export]
public partial class MainWindow : Window
{
    private CompositionContainer _container;
    private IRegionManager _regionManager;
    IRegion _region;

    ViewA _viewA;
    ViewB _viewB;

    [ImportingConstructor]
    public MainWindow(CompositionContainer container, IRegionManager regionManager)
    {
        InitializeComponent();
        _container = container;
        _regionManager = regionManager;

        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        _viewA = _container.GetExportedValue<ViewA>();
        _viewB = _container.GetExportedValue<ViewB>();

        _region = _regionManager.Regions["ContentRegion"];

        _region.Add(_viewA);
        _region.Add(_viewB);
    }
}

我遇到的问题是MainWindow无法初始化,在尝试在ContentControl中设置RegionName时抛出System.Windows.Markup.XamlParseException 内部异常消息是

  

尝试获取类型实例时发生激活错误   DelayedRegionCreationBehavior,key&#34;&#34;

此处有例外全部详情

  

发生了System.Windows.Markup.XamlParseException   HResult = -2146233087 LineNumber = 12 LinePosition = 10 Message =&#39; Set   属性&#39; Prism.Regions.RegionManager.RegionName&#39;抛出异常。&#39;   行号&#39; 12&#39;和行位置&#39; 10&#39;   Source = PresentationFramework StackTrace:          在System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,Boolean   skipJournaledProperties,Object rootObject,XamlObjectWriterSettings   设置,Uri baseUri)          在System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,Boolean skipJournaledProperties,Object rootObject,   XamlAccessLevel accessLevel,Uri baseUri)          在System.Windows.Markup.XamlReader.LoadBaml(Stream stream,ParserContext parserContext,Object parent,Boolean closeStream)          在System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator)          在C:\ Users \ ralfaro \ Documents \ Visual Studio中的DuoApp_ViewNav.MainWindow.InitializeComponent()   2015 \ Projects \ DuoApp_ViewNav \ DuoApp_ViewNav \ MainWindow.xaml:第1行
  的InnerException:          的HResult = -2146233088          消息=尝试获取DelayedRegionCreationBehavior类型的实例时发生激活错误,键&#34;&#34;          来源= Microsoft.Practices.ServiceLocation          堆栈跟踪:               在Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type   serviceType,String key)in   C:\项目\ CommonServiceLocator \主\ Microsoft.Practices.ServiceLocation \ ServiceLocatorImplBase.cs:线   53               在Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstanceTService   在   C:\项目\ CommonServiceLocator \主\ Microsoft.Practices.ServiceLocation \ ServiceLocatorImplBase.cs:线   90               在Prism.Regions.RegionManager.CreateRegion(DependencyObject元素)               在Prism.Regions.RegionManager.OnSetRegionNameCallback(DependencyObject   element,DependencyPropertyChangedEventArgs args)               在System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs)   E)               在System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs)   E)               在System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs)   参数)               在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex   entryIndex,DependencyProperty dp,PropertyMetadata元数据,   EffectiveValueEntry oldEntry,EffectiveValueEntry&amp; newEntry,布尔值   coerceWithDeferredReference,Boolean coerceWithCurrentValue,   OperationType operationType)               在System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp,   对象值,PropertyMetadata元数据,布尔值   coerceWithDeferredReference,Boolean coerceWithCurrentValue,   OperationType operationType,Boolean isInternal)               在System.Windows.Baml2006.WpfMemberInvoker.SetValue(对象实例,   对象值)               在MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember成员,   对象obj,对象值)               在MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst,   XamlMember属性,对象值)          的InnerException:               的HResult = -2146233088               消息=尝试获取DelayedRegionCreationBehavior类型的实例时发生激活错误,键&#34;&#34;               来源= Microsoft.Practices.Prism.MefExtensions               堆栈跟踪:                    在Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(类型   serviceType,String key)                    在Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type   serviceType,String key)in   C:\项目\ CommonServiceLocator \主\ Microsoft.Practices.ServiceLocation \ ServiceLocatorImplBase.cs:线   49               的InnerException:

老实说,我不知道为什么抛出这个异常。我错过了使用MefBootstrapper工作的东西吗?

1 个答案:

答案 0 :(得分:0)

终于明白了。我错误地引用了两个具有MefBootstrapper实现的程序集,Prism.Mef和Prism.MefExtensions。删除Prism.MefExtensions包/引用后,应用程序开始正常工作。 我上次发现的最后一个故障排除步骤是在本地运行Prism库代码并从那里添加所需的引用,这使我能够识别出不需要MefExtensions。

我的packages.config文件的当前版本如下所示:

Interviewees

我希望这有助于其他人。