开发者

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

开发者 https://www.devze.com 2022-12-19 03:56 出处:网络
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 de

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(Assembl开发者_如何学JAVAyName 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()


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}">
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号