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


Wtf(暫時命名,隨便起的 = _=)(現在已經命名為CPF了),模仿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暫時不提供下載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM