好像没有什么好的方法去继承一个自定义控件。

树莓派 2009-11-22 01:21:25
我不知道怎么去继承一个自定义控件。就是说有一个MyControl继承与UserControl,然后我又想要写一个控件继承MyControl
国外的文章说,如果一个类没有XMAL,纯代码的话,是可以继承的。
如果真的要继承一个XMAL和Code-Behide的类,有一个很麻烦的方法。
不知道大家有什么好方法吗?
...全文
68 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
MicroYee 2009-11-23
  • 打赏
  • 举报
回复
assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "SL2Controls")
这个标记一下
diandian82 2009-11-23
  • 打赏
  • 举报
回复
learn
xingjunli 2009-11-23
  • 打赏
  • 举报
回复
目前是这样的,只能使用类+ControlTemplate的方式
jv9 2009-11-23
  • 打赏
  • 举报
回复
按照下面的方法也可以实现:

1。 建立个新的Silverlight项目,SL2Controls

2。 添加一个类BaseControl

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL2Controls {
public class BaseControl : UserControl {

}
}


3。 添加一个UserControl,叫做InheritedControl


<BaseControl x:Class="SL2Controls.InheritedControl"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Red">

</Grid>
</BaseControl>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL2Controls {
public partial class InheritedControl : BaseControl {
public InheritedControl() {
InitializeComponent();
}
}
}



4。在SL2Controls项目中的AssemblyInfo.cs文件中添加映射

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Markup;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SL2Controls")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SL2Controls")]
[assembly: AssemblyCopyright("Copyright © 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "SL2Controls")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("27885afb-0308-473e-9773-73350bc1555f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]



5。 在例程页面添加继承控件。


<UserControl x:Class="SL2Controls.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myctl="clr-namespace:SL2Controls"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<myctl:InheritedControl/>
</Grid>
</UserControl>

Error_Code 2009-11-23
  • 打赏
  • 举报
回复
能写到xaml里的东西都可以用.cs的代码写出来
不用xaml不就行了 就可以被继承了
纷享刘晨 2009-11-22
  • 打赏
  • 举报
回复
对,是这个样子。如果你继承已有控件,使用类+ControlTemplate的方式是最好的

对于UserControl,目前没有找到什么好办法。对于特定需要实现的某一类自定义控件,我们使用接口的方式

你自己的自定义控件(包含xaml)只能继承自UserControl,但是可以实现多个你需要的接口

不是很方便,算是变相解决吧

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧