Compile Error: 'ResourceDictionary' Root Element Is a Generic Type

February 22nd 2015 WPF XAML Build Error

Strange Compiler Error

One of my coworkers suddenly encountered the following error in a large .NET application we're developing:

'ResourceDictionary' root element is a generic type and requires a x:Class attribute to support the x:TypeArguments attribute specified on the root element tag.

Of course the ResourceDictionary class in question wasn't generic, hence the error made no sense. To make matters worse, no changes were made to the file since it was checked out from source control; and that version of the project compiled fine.

  • We started randomly reverting different changes in the working copy - without success.
  • We tried to compile the same code on a different machine, just to make sure there wasn't something wrong with the developer's copy of Visual Studio - it failed the same way on other machines.

Eventually we ran out of ideas and decided to check whether anyone else has encountered this issue before. Sure enough, we found a MSDN forum post with exactly the same issue. The solution for the problem made no more sense than the problem itself: add the x:Class attribute to the root element as suggested in the error message. Its value doesn't really matter and can be anything.

How to Reproduce the Issue

This was enough to resolve our issue, but I couldn't let the matter rest without investigating it further. After reading the forum thread in detail a couple of times, I started looking for a simple way to reproduce the issue in another project. It turned out you need at least 3 classes in your project:

  • A generic class derived from UserControl:
public class GenericBaseUserControl<T> : UserControl
{
    public T Property { get; set; }
}
  • A custom user control deriving from it:
<genericResourceDictionary:GenericBaseUserControl x:TypeArguments="system:String"
    x:Class="GenericResourceDictionary.CustomUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:genericResourceDictionary="clr-namespace:GenericResourceDictionary"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

    </Grid>
</genericResourceDictionary:GenericBaseUserControl>
  • A ResourceDictionary importing the namespace with the above custom control:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:GenericResourceDictionary">
</ResourceDictionary>

Known Workarounds

That's enough for the bogus compile error to appear. I know of 2 workarounds to avoid the issue:

  • Remove the namespace containing the problematic custom control from the resource dictionary if you don't need it:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
                    <!-- No GenericResourceDictionary namespace, no compile error -->
</ResourceDictionary>
  • Add the x:Class attribute to the ResourceDictionary element:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:GenericResourceDictionary"
                    x:Class="Whatever"> <!-- This attribute fixes the build -->
</ResourceDictionary>

In spite of these known workarounds I decided to report the issue to Microsoft Connect. I don't want other developers wasting their time on this strange compiler error, like we did. Feel free to upvote it, if your opinion is the same.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License