Known Issue – Controls Deriving From A Generic Base Class Must Be In Separate Assembly
There is a known issue when using the Visual Studio 2010 WPF & Silverlight Designer or XAML Editor and a control derives from a generic base class. The control and generic base class must be located in separate assemblies, otherwise the Designer and XAML Editor will display the following error:
Error 1 The type 'local:GenericBase' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built
The listed error will not prevent you from using the Designer or building your application. However, its a misleading error message that does not help you correct the design-time problem.
Workaround
To workaround this issue, you’ll need to place the generic base class in a separate assembly from the control as pictured below.
In this example the UserControlWithGenericBase derives from GenericBase and the two types are located in separate assemblies.
TypeArguments Property
In the above class diagram you can see that UserControlWithGenericBase derives from:
- C# GenericBase<String>
- VB – GenericBase(Of String)
Notice the third line of XAML, the x:TypeArguments property is set to sys:String. This matches the generic type argument passed to GenericBase in the above class diagram and the UserControlWithGenericBase UserControl class declaration below.
x:TypeArguments is required when the base class is a generic type.
<gbc:GenericBase
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:TypeArguments="sys:String"
xmlns:gbc="clr-namespace:GenericBaseClassLibrary;assembly=GenericBaseClassLibrary"
x:Class="UserControlDerivingFromGenericTypeCS.UserControlWithGenericBase"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</gbc:GenericBase>
C# Code
public partial class UserControlWithGenericBase : GenericBase<String> {
public UserControlWithGenericBase() {
InitializeComponent();
}
}
VB.NET Code
Public Class UserControlWithGenericBase
Inherits GenericBase(Of String)
End Class
Silverlight
This workaround only applies to WPF applications. Currently Silverlight does not support the x:TypeArguments property that is required in the deriving control’s XAML root tag.
If you must have Silverlight controls that derive from a generic base class you have to do some extra work. Basically you need to have an extra class in the middle so that the UserControl would derive from a non-generic class
Base class: public class GenericBase<T> : UserControl
Middle class: public class MiddleStringControl : GenericBase<String>
UserControl: public class UserControlWithGenericBase : MiddleStringControl
Comments
Microsoft values your opinion about our products and documentation. In addition to your general feedback it is very helpful to understand:
- How the above feature enables your workflow
- What is missing from the above feature that would be helpful to you
Thank you for your feedback and have a great day,
Karl Shifflett
Visual Studio Cider Team
Comments
Anonymous
January 24, 2010
Thanks Karl. If you use a virtual machine and your code is located on the host machine, you may want to add <loadFromRemoteSources enabled="true"/> to devenv.exe.config to avoid additional errors. See http://msdn.microsoft.com/en-us/library/dd409252%28VS.100%29.aspxAnonymous
February 12, 2010
I'm disappointed to see that in Visual Studio 2010 RC all the content of the XAML documents is still completely underlined with blue squiggles. It's really annoying to read documents like that. The tooltip says: 'Cannot create an instance of "GenericBase`1"'.Anonymous
February 15, 2010
Fabrice, Are you implying that all your XAML documents have squiggles or just when the Generic Base Class issue is affecting your XAML. After moving your Generic Base Class to another assembly, you should not be seeing this. Please let me know under what circumstances you are having this problem. KarlAnonymous
February 18, 2010
The situation is the following:
- I have two projects in a solution
- One of the projects contains my generic base class (is it in that cas what you call an external assembly?)
- The other project consumes this base class in xaml files
- When I'm editing these xaml files, I get the squiggles all over. Maybe not all the time though, but most of the time. I don't think that this is expected when you say that moving the generic base class to another assembly should fix the issue. Fabrice
Anonymous
February 18, 2010
The comment has been removedAnonymous
March 21, 2010
Hi, Actually for me this is more than just an annoyance, it provides an actual compiler error. This issue seems to have been around in different incarnations ever since SL2 and I'm surprised it hasn't been put to bed yet. There is a Connect support issue open about it that was closed due to 'other priorities'. I'm a bit disappointed by this; creating a ViewBase<T> is a pretty common thing to do these days so the XAML designer needs to get better generic support. I also tried your suggested solution just in case. But it didn't work. I'm using VS2010 RC with SL3 project. Cheers SteveAnonymous
March 22, 2010
Steve, Silverlight does not have support for Generics in XAML yet. I would raise this issue again on the Silverlight forums or Connect and request this feature for Silverlight 5. Have a great day and thank you for your feedback. KarlAnonymous
January 15, 2013
I had a very similar issue that was resolved by moving my control to an external assembly. The differences were as follows. I am using Silverlight 5. I am using Visual Studio 2012 Update 1 My base class is not generic (it is System.Windows.Controls.DataGrid).