VS2008 XAML Designer exception “Key cannot be null” when using default styles from a ResourceDictionary

强颜欢笑 提交于 2019-12-11 06:53:11

问题


I'm using a shared ResourceDictionary to define default styles and having major conflicts with the XAML Designer in Visual Studio 2008. Key cannot be null appears for all attempts to show the XAML design view.

The dictionary is merged into App.xaml (to be used by all windows) and has a number of styles setting the defaults for controls, so they are defined as shown below without a key. If you add an x:Key attribute to the styles, they are no longer applied by default. I don't want to have to put explicit Style clauses on every control but it looks like I might have to.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="windowStyle" TargetType="{x:Type Window}">
        <Setter Property="Background" Value="LightGray" />
    </Style>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="AliceBlue" />
        <Setter Property="Height" Value="23" />
    </Style>
</ResourceDictionary>

The full exception error message with stack:

Key cannot be null.
Parameter name: key
   at System.Collections.Hashtable.get_Item(Object key)
   at MS.Internal.Xaml.AssemblyNode.For(Assembly assembly, Boolean includeInternal)
   at MS.Internal.Xaml.ReflectionProjectNode.LoadAssembly(AssemblyName name, Boolean includeInternal)
   at MS.Internal.Xaml.ReflectionProjectNode.BuildAssemblies()
   at MS.Internal.Xaml.ReflectionProjectNode.BuildSubsumption()
   at MS.Internal.Xaml.ReflectionProjectNode.SubsumingNamespace(Identifier identifier)
   at MS.Internal.Xaml.XmlElement.BuildScope(PrefixScope parentScope, IParseContext context)
   at MS.Internal.Xaml.XmlElement.FindElementType(PrefixScope parentScope, IParseContext context)
   at MS.Internal.DocumentTrees.Markup.XamlSourceDocument.get_RootType()
   at Microsoft.Windows.Design.Documents.Trees.MarkupDocumentTreeManager.get_RootType()
   at Microsoft.Windows.Design.Documents.MarkupDocumentManager.CalculateLoadErrorState()
   at Microsoft.Windows.Design.Documents.MarkupDocumentManager.get_LoadState()
   at MS.Internal.Host.PersistenceSubsystem.Load()
   at MS.Internal.Host.Designer.Load()
   at MS.Internal.Designer.VSDesigner.Load()
   at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedView.Load()
   at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedDesignerFactory.Load(IsolatedView view)
   at MS.Internal.Host.Isolation.IsolatedDesigner.BootstrapProxy.LoadDesigner(IsolatedDesignerFactory factory, IsolatedView view)
   at MS.Internal.Host.Isolation.IsolatedDesigner.BootstrapProxy.LoadDesigner(IsolatedDesignerFactory factory, IsolatedView view)
   at MS.Internal.Host.Isolation.IsolatedDesigner.Load()
   at MS.Internal.Designer.DesignerPane.LoadDesignerView()

回答1:


I'm not sure why it would require a key, but you can try the following

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">


来源:https://stackoverflow.com/questions/2189131/vs2008-xaml-designer-exception-key-cannot-be-null-when-using-default-styles-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!