views:

189

answers:

1

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"&gt;
    <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  A: 

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}">
Thomas
Yep, that's it. Thanks, I was trying to remember the format of the "default key" to try the same thing myself.
Andy Dent
AARRRGGGHHH! It's back and the apparent success of that key clause was not the answer, although it does seem to make it a bit more stable. Rebuilding seems to fix about 80% of the time.
Andy Dent
Ahh it's one of those random errors, all I can suggest is maybe creating a new project and slowly transferring your code over to see if you can find what's causing the error
Thomas