当前位置: 移动技术网 > IT编程>开发语言>.net > 造轮子,模仿WPF的UI框架,还没完善。。。

造轮子,模仿WPF的UI框架,还没完善。。。

2018年08月22日  | 移动技术网IT编程  | 我要评论

黄金 走势,翟鸿燊视频讲座全集,爱滋病初期症状

wtf(暂时命名,随便起的 = _=),模仿wpf的框架,还没有完善,只有简单的基础元素,支持数据绑定。虽然支持mono但是mono有bug

写这个只是兴趣爱好,感觉也没多大意义了,如果这个ui框架完善了,有多少人愿意用?毕竟windows上有wpf,而且c#跨平台需求也不多啊。我对wpf也不算熟悉,要完善的话,还有很多要写。一大堆常用控件和设计器。不过我不用xml来描述,而是直接用c#来定义,设计器直接生成c#代码,因为我觉得,如果有强大的设计器,写xml就是多余的,而且解析xml还影响性能,对于wpf,我觉得xaml太啰嗦了。

 

wtfobject  相当于wpf里的dependencyobject依赖对象。继承该类的对象,所有属性默认都是依赖属性

属性写法:

 

        /// <summary>
        /// 绑定的数据上下文
        /// </summary>
        [uipropertymetadata(null, true)]
        public virtual object datacontext
        {
            get { return getvalue<object>(); }
            set { setvalue(value); }
        }

 

属性上的特性可以是 propertymetadata或者uipropertymetadata 中的一个,默认值可以通过这两个特性来设置。如果不加这两个特性,那默认值就是null或者0

如果是复杂属性类型默认值,可以通过重写 onoverridemetadata 来设置

 

       protected override void onoverridemetadata(overridemetadata overridepropertys)
       {
            base.onoverridemetadata(overridepropertys);
            overridepropertys.override("strokestyle", new uipropertymetadataattribute(new stroke(1), false, false, true));
        }

 

 

数据绑定:

var bind = 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["text"] <= button["test"];//右到左数据绑定

var bind = label["text"] >= button["test"];//左到右数据绑定

var bind = label["text"] != button["test"];//右到左数据绑定,只传递一次

var bind = label["text"] == button["test"];//双向绑定

 

 

命令绑定:

当事件触发或者属性变化的时候调用方法

label.addcommand("mousedown","button1_click","commandcontext", wtf.windows.commandparameter.eventargs);
        /// <summary>
        /// 添加处理命令,命令方法在commandcontext或者其他属性的对象上
        /// </summary>
        /// <param name="eventname">触发的事件名或者属性名</param>
        /// <param name="methodname">命令方法名</param>
        /// <param name="propertyname">命令对象所在的属性名</param>
        /// <param name="ps">方法参数,可以是自定义的数据或者相关属性或者事件的数据</param>
        public void addcommand(string eventname, string methodname, string propertyname = "commandcontext", params object[] ps)

 

 

一些类型的隐式转换

brush, color :  "#0000ff" "#ff0000ff" “255,255,255”  “255,255,255,255”  颜色字符串转换,按顺序是r,g,b、a,r,g,b

 

floatvalue: "10%" “100” "zero" "auto"  100  100.5     数字或者百分比字符串转换,整形,浮点数据自动转换

 

触发器样式例子

 

按钮的鼠标操作效果,鼠标移入移出按下背景色变化

            

 

       styling.trigger hover = new styling.trigger { condition = styling.conditions.equals, property = "ismouseover", value = true };

            hover.setters.add("background", drawing.brush.parse("#ff0000"));

            styling.trigger normal = new styling.trigger { };

            normal.setters.add("background", drawing.brush.parse("#00ff00"));

            styling.trigger press = new styling.trigger { condition = styling.conditions.equals, property = "ismousecaptured", value = true };

            press.setters.add("background", drawing.brush.parse("#ffff00"));

            label.triggers.add(normal);

            label.triggers.add(hover);

            label.triggers.add(press);

            label.mousedown += delegate
            {
                label.capturemouse();
            };

            label.mouseup += delegate
            {
                label.releasemousecapture();
            };

 

 

wtfobject 的属性设置的值优先级比触发器样式设置的值要高,所以当你设置了属性值,触发器样式可能没有效果

 

 

添加ui元素,ui元素可以互相嵌套

            var root = testcontrol1.rootuielement;
            root.foreground = "#ff0000";
            root.fontfamily = "微软雅黑";
            root.fontsize = 16;
            root.children.add(label);
            root.children.add(new windows.shapes.ellipse
            {
                stroke = "#0000ff",
                fill = "white",
                width = 40,
                height = 20,
                marginleft = 30,
                margintop = 30
            });

            root.children.add(new windows.shapes.ellipse
            {
                stroke = "#0000ff",
                fill = "white",
                width = 40,
                height = 20,
                marginright = "30%",
                margintop = 30

            });    

 

 

元素布局,支持百分比布局,margin调整定位,默认居中。

 

触发器绑定动画

            var t = new trigger();
            storyboard ss = new storyboard();
            ss.duration = new timespan(0, 0, 0, 0, 500);
            var tl = new timeline(1);
            tl.keyframes.add(new keyframe<floatvalue> { property = "height", value = 300, ease = new bounceease(), animatemode = animatemode.easein });
            tl.keyframes.add(new keyframe<floatvalue> { property = "width", value = "30%", ease = new bounceease(), animatemode = animatemode.easein });
            tl.keyframes.add(new keyframe<generaltransform> { property = "rendertransform", animatemode = animatemode.easeout, value = new generaltransform { angle = 30 }, ease = new elasticease() });
            //tl.keyframes.add(new keyframe<solidcolorbrush> { property = shape.fillproperty, value = "white" });
            ss.timelines.add(tl);
            t.property = "ismouseover";
            t.value = true;
            t.animation = ss;
            t.setters.add("fill", brush.parse("#fff"));
            v.triggers.add(t);

 

如果写自定义控件,继承wtf.windows.controls.control  然后重写initializecomponent 把样式定义代码写在里面,如果再次继承修改的话,可以重写覆盖。

dll暂时不提供下载

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网