之前在网上看到很多关于组件开发的资料,但真正可以用到框架内的却很少。今天贴出自己做的组件,并适合大部分框架的代码。
组件开发需要先做出组件的基类,然后由其他的各类组件去继承这个基类,下面是组件基类的代码。
组件基类:
public abstract class Component { public abstract string Name { get; } public abstract string Icon { get; } public abstract string Description { get; } public abstract ComponentEvent ComponentEvent { get; } public abstract ComponentProperty ComponentProperty { get; } public abstract UIElement CreateUIElement(); }
组件基类的属性:
public abstract class ComponentProperty : INotifyPropertyChanged { private double width; private double height; private double left; private double top;private string name; [Category("基本")] [Description("名称")] public string Name { get { return name; } set { if (name == value) return; name = value; OnPropertyChanged("Name"); } } [Category("大小")] [Description("宽度")] public double Width { get { return width; } set { if (width == value) return; width = value; OnPropertyChanged("Width"); } } [Category("大小")] [Description("高度")] public double Height { get { return height; } set { if (height == value) return; height = value; OnPropertyChanged("Height"); } } [Category("位置")] [Description("距左")] public double Left { get { return left; } set { if (left == value) return; left = value; OnPropertyChanged("Left"); } } [Category("位置")] [Description("距上")] public double Top { get { return top; } set { if (top == value) return; top = value; OnPropertyChanged("Top"); } } #region INotifyPropertyChanged Members //属性的监听事件 public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName"); PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } #endregion }
组件基类的事件:
public abstract class ComponentEvent : INotifyPropertyChanged{#region 这个可以去掉private string _Click;public string Click{ get{ return _Click;} set{ if(_Click==value)return; _Click=value; OnPropertyChanged("Click"); }}public void OnClick(object sender,RoutedEventArgs e){ if(Click!=null){ //事件的处理 }}#endregion#region INotifyPropertyChanged Members//监听事件public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");PropertyChangedEventHandler handler = PropertyChanged;if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));}#endregion}
以上是组件的基类,组件基类打包为模板以及组件开发请关注后续章节,加群可优先哦。
WPF、AE技术交流群:94234450 点击加入QQ群: