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

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.