造輪子,模仿WPF的UI框架,還沒完善。。。

Wtf(暫時命名,隨便起的 = _=),模仿WPF的框架,尚未完善,只有簡單的基礎元素,支持數據綁定。雖然支持mono可是mono有bug框架

寫這個只是興趣愛好,感受也沒多大意義了,若是這個UI框架完善了,有多少人願意用?畢竟Windows上有WPF,並且C#跨平臺需求也很少啊。我對WPF也不算熟悉,要完善的話,還有不少要寫。一大堆經常使用控件和設計器。不過我不用XML來描述,而是直接用C#來定義,設計器直接生成C#代碼,由於我以爲,若是有強大的設計器,寫XML就是多餘的,並且解析XML還影響性能,對於WPF,我以爲Xaml太囉嗦了。ide

 

WtfObject  至關於WPF裏的DependencyObject依賴對象。繼承該類的對象,全部屬性默認都是依賴屬性佈局

屬性寫法:性能

 

        /// <summary>
        /// 綁定的數據上下文
        /// </summary>
        [UIPropertyMetadata(null, true)]
        public virtual object DataContext
        {
            get { return GetValue<object>(); }
            set { SetValue(value); }
        }

 

屬性上的特性能夠是 PropertyMetadata或者UIPropertyMetadata 中的一個,默認值能夠經過這兩個特性來設置。若是不加這兩個特性,那默認值就是null或者0動畫

若是是複雜屬性類型默認值,能夠經過重寫 OnOverrideMetadata 來設置spa

 

       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"];//雙向綁定

 

 

命令綁定:雙向綁定

當事件觸發或者屬性變化的時候調用方法code

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)

 

 

一些類型的隱式轉換orm

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暫時不提供下載

相關文章
相關標籤/搜索