CPF(暂时命名)(Cross platform framework),模仿WPF的框架,支持NETCore的跨平台UI框架,暂时不够完善,只用于测试,暂时只支持Windows和Mac。支持数据绑定,CSS,动画。。。windows
可能有人会说,不是有个开源的Avalonia ,我试过,不过他的性能不行,启动速度慢,内存占用高,附带的dll一大堆,他的是Xaml来描述UI的,个人不提供Xaml,直接用C#来写,之后将出设计器直接生成C#代码。框架
CpfObject至关于WPF里的DependencyObject依赖对象。继承该类的对象,全部属性默认都是依赖属性ide
属性写法:布局
1 /// <summary> 2 /// 绑定的数据上下文 3 /// </summary> 4 [PropertyMetadata(null)] 5 public object DataContext 6 { 7 get { return GetValue<object>(); } 8 set { SetValue(value); } 9 }
属性上的特性能够是 PropertyMetadata或者UIPropertyMetadata 中的一个,默认值建议经过这两个特性来设置。若是不加这两个特性,那默认值就是null或者0性能
若是是复杂属性类型默认值,能够经过重写 OnOverrideMetadata 来设置测试
protected override void OnOverrideMetadata(OverrideMetadata overridePropertys) { base.OnOverrideMetadata(overridePropertys); overridePropertys.Override("StrokeStyle", new UIPropertyMetadataAttribute(new Stroke(1))); }
附加属性:优化
/// <summary> /// 获取或设置元素行索引 /// </summary> public static Attached<int> RowIndex { get { return RegisterAttached(0); } } Grid.RowIndex(control, 1);//使用附加属性方式设置行索引 var index = Grid.RowIndex(control);//获取附加属性值
数据绑定:动画
var bind = label[nameof(Label.Text)] <= "Test";//右到左数据绑定,数据源是DataContext的属性 var bind = label["Text"] >= "Test";//左到右数据绑定,数据源是DataContext的属性 var bind = label["Text"] != "Test";//左到右数据绑定,只传递一次 ,数据源是DataContext的属性 var bind = label["Text"] == "Test";//双向绑定,数据源是DataContext的属性,双向绑定须要对象实现INotifyPropertyChanged var bind = label[nameof(Label.Text)] <= button["Test"];//右到左数据绑定 var bind = label[nameof(Label.Text)] >= button["Test"];//左到右数据绑定 var bind = label[nameof(Label.Text)] != button["Test"];//左到右数据绑定,只传递一次 var bind = label[nameof(Label.Text)] == button["Test"];//双向绑定 btn.Bindings.Add(nameof(Button.Content), nameof(TextBlock.Text), null, BindingMode.OneWay, a => a.ToString());//经过Bindings属性添加绑定,建议用nameof()这样不容易写错
命令绑定:spa
当事件触发或者属性变化的时候调用方法设计
Label.Commands.Add(nameof(Window.MouseDown), nameof(Window.DragMove), Relation.Me.Parent);
new Button { Width = 20, Height = 20, MarginRight = 30, MarginTop = 5, Content = "Test", Commands = { { nameof(Button.MouseUp), ()=> { window.WindowState = WindowState.Minimized; } } } }
布局系统
布局流程和WPF差很少,先Measure再Arrange,若是自定义布局容器,能够参考WPF的代码
元素布局,支持百分比布局,margin调整定位,默认居中。至关于CSS里中的绝对定义position: absolute;
MarginLeft,MarginTop,MarginRight,MarginBottom,通常默认值是Auto,当设置值以后固定对应边到父容器到内边距的距离
Width,Height,通常默认值也是Auto,若是没设置,实际尺寸由内容或者子元素尺寸决定,或者由Margin决定
new Border { Width = "100%", Height = "100%", Background = "#fff", }
属性值的自动转换:
Width = "100%"; Width = "Auto"; Width = 100; Background = Color.FromRgb(100,100,100); Background = "#fff"; Background =image;
CSS样式
支持简单的选择器
TextBlock { Foreground:rgb(255,0,0);} 选择全部TextBlock类型的元素
.test{Foreground:rgb(255,0,0);} 选择全部包含test 类名的元素,类名经过Classes属性添加
#test{Foreground:rgb(255,0,0);} 选择全部Name属性为test的元素
[IsMouseOver=true]{…} 添加触发器
Button TextBlock{…} Button里的后代为TextBox的元素,只支持两层
Button>TextBlock{…} Button直接子元素为TextBox的元素,只支持两层
触发器和动画
.test[IsMouseOver=true]{animation-name:myfirst;animation-duration:1s;animation-iteration-count: 1;}
@keyframes myfirst
{
0% {Background: #f00;}
25% {Background: #0ff;}
50% {Background: #00f;}
100% {Background: #0f0;}
}
经过根元素的LoadStyle方法加载样式,好比Window对象
控件模板:
继承你要修改的控件,而后重写InitializeComponent 把定义代码写在里面,不知道怎么定义?查看内置模板代码,详细模板代码看压缩包里的文档,复制过去,本身根据须要修改
对Mac开发不熟悉,Mac系统下还不能输入中文,有没有猛男赐教一下,怎么调用输入法,打开关闭输入法和控制输入法候选词位置
我感受模板设计的不够好,还有数据绑定还能够优化一下。各位有什么想法和意见说说。