C# Silverlight用户控件部分类的继承
我试图允许几个类继承更通用的Silverlight用户控件,以避免代码中的冗余。这些类继承扩展控件,扩展控件随后继承用户控件类。我遇到的问题是,ExtendedControlExtension.g.cs文件在每次编译时都会重新生成,继承错误(它继承的是用户控件,而不是我的扩展控件) 请注意,我一直在.cs和g.cs文件中继承扩展控件,但在.aspx文件中继续使用用户控件标记,因为这会导致错误 错误29 XML命名空间中不存在标记“ExtendedControl”http://schemas.microsoft.com/winfx/2006/xaml/presentation" 有办法解决这个问题吗C# Silverlight用户控件部分类的继承,c#,silverlight,multiple-inheritance,partial-classes,user-controls,C#,Silverlight,Multiple Inheritance,Partial Classes,User Controls,我试图允许几个类继承更通用的Silverlight用户控件,以避免代码中的冗余。这些类继承扩展控件,扩展控件随后继承用户控件类。我遇到的问题是,ExtendedControlExtension.g.cs文件在每次编译时都会重新生成,继承错误(它继承的是用户控件,而不是我的扩展控件) 请注意,我一直在.cs和g.cs文件中继承扩展控件,但在.aspx文件中继续使用用户控件标记,因为这会导致错误 错误29 XML命名空间中不存在标记“ExtendedControl”http://schemas.mi
谢谢 您不能更改
.g.cs
文件,事实上文件中就这么说了。另外,使用术语“自定义控件”是很不幸的,因为这意味着一些特定的内容,而不是您要做的事情。但是,好消息是,你正在尝试做的是可能的
从UserControl
派生:
public class FancyUserControl : UserControl
{
// Your added common functionality.
}
然后使用正常机制将新的UserControl
添加到项目中,比如说UserControl1
。然后编辑UserControl.xaml
文件,如下所示:
<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>
public partial class UserControl1 : FancyUserControl
{
public UserControl1()
{
InitializeComponent();
}
}
VisualStudio还不会很高兴,但最终会重建您的项目,一切都会好起来
类
UserControl1
现在是从FancyUserControl
派生的,而不是UserControl
,您可以开始添加常用功能。要添加更多控件,在最初将每个新控件添加到项目中后,您需要手动编辑XAML和代码隐藏一次。感谢您提供了非常完整的答案(我知道您不应该编辑.gs文件,但在您回答问题之前,这是实现所需功能的唯一方法)!