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