I am trying to allow several classes to inherit a more general Silverlight user control to avoid redundancy in my code. The classes inherit the extended control, which then inherits the User Control class. The issue I have been running into is that the ExtendedControlExtension.g.cs file regenerates every time I compile, with the incorrect inheritance (it inherits User Control not my Extended Control).
Note that I have been inheriting the Extended Control in the .cs and g.cs files, but continuing to use the User Control tag in the .aspx file as this causes the error
Error 29 The tag ‘ExtendedControl’ does not exist in XML namespace ‘http://schemas.microsoft.com/winfx/2006/xaml/presentation’.
Is there a way to fix this?
Thanks!
You cannot change the
.g.csfile, in fact is says so right in the file. Also, it’s unfortunate to use the term “custom control” as this means something specific and not what you are trying to do. But, the good news is that what you are trying to do is possible.Derive from
UserControl:and then add a new
UserControlto your project using the normal mechanism, let’s sayUserControl1. Then edit theUserControl.xamlfiles as follows:paying special attention to the three lines with
localin them, adjusting to your application. Then edit theUserControl1.xaml.csfile as follows:and Visual Studio won’t be quite happy yet but finally rebuild your project and all will be well.
The class
UserControl1is now derived fromFancyUserControlinstead ofUserControland you can begin adding your common functionality. To add more controls you will need to manually edit the XAML and code-behind once after initially adding each new control to the project.