Inheritance with Silverlight User Control Partial Classes

I am trying to allow multiple classes to inherit a more general Silverlight control in order to avoid redundancy in my code. Classes inherit an extended control, which then inherits the User Control class. The problem that I encountered is that the ExtendedControlExtension.g.cs file is restored every time it is compiled with the wrong inheritance (it inherits User Control, not my extended control).

Note that I inherited Extended Control in the .cs and g.cs files, but I continue to use the User Control tag in the .aspx file, as this causes an error

Error 29 The tag 'ExtendedControl' does not exist in the XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

Is there any way to fix this?

Thanks!

+6
source share
1 answer

You cannot modify the .g.cs file, it is actually said so correctly in the file. Also, unfortunately, use the term "user control" because it means something specific, not what you are trying to do. But the good news is that what you are trying to do is possible.

Output from UserControl :

 public class FancyUserControl : UserControl { // Your added common functionality. } 

and then add a new UserControl to your project using the usual mechanism, say UserControl1 . Then edit the UserControl.xaml files as follows:

 <local:FancyUserControl x:Class="SilverlightApplication1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SilverlightApplication1" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> </Grid> </local:FancyUserControl> 

paying particular attention to the three lines with local in them, adapting to your application. Then edit the UserControl1.xaml.cs file as follows:

 public partial class UserControl1 : FancyUserControl { public UserControl1() { InitializeComponent(); } } 

and Visual Studio will not be happy yet, but finally rebuild your project and everything will be fine.

The UserControl1 class is now derived from FancyUserControl instead of UserControl , and you can start adding your common functions. To add additional controls, you need to manually edit the XAML and code after once after the initial addition of each new control to the project.

+9
source

Source: https://habr.com/ru/post/888063/


All Articles