C# 屬性綁定

來源:互聯網
上載者:User

標籤:c# 屬性綁定

C#的反射明顯的比AS3的反射要強很多啊。可以從外部存取到類的私人成員。舉個例子:

                Type _class = this.GetType();                FieldInfo _field = _class.GetField(@fieldName, BindingFlags.Instance | BindingFlags.NonPublic);

枚舉 : BindingFlags.NonPublic ->非公用成員將包括在內進行搜尋,意思就是private成員也可以搜尋到。


在model中註冊鬚髮的主題(User)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using BindLib.com;namespace BindDemo.com.model{    public sealed class User : BaseDataModel    {        private int _hp = 1000;        public int Hp        {            get { return _hp; }            //綁定hp(血量)            set { this.ChangeValue<int>("Hp","_hp",value); }        }        private bool _isLive = false;        public bool IsLive        {            get { return _isLive; }            set { this.ChangeValue<bool>("IsLive", "_isLive", value); }        }    }}

這的注意的是欄位_hp和欄位_isLive都是private(私用的): 這個和AS3綁定(見 AS3 屬性綁定/上一篇)有很大的不同,這得益於C#可以從外部存取private


view中需要綁定(註冊偵聽)(UserView)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using BindLib;namespace BindDemo.com.model{    public sealed class UserView    {        public User user;        private int _hp;        public int Hp        {            set {                 this._hp = value ;                Console.WriteLine("血量 {0}", value);            }            get { return this._hp; }        }        public UserView()        {            user = new User();            BindTools.BindProperty<int>(this, "Hp", user, "Hp", true);            BindTools.BindSetter<bool>(this.UpdateIsLive, user, "IsLive",true);        }        private void UpdateIsLive(bool @isLive)        {            Console.WriteLine(@isLive ? "活著" : "戰死");        }    }}

好了 , 具體參數意思可以參考(《AS3 屬性綁定》篇,上篇)

測試:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using BindDemo.com.model;namespace BindDemo{    class Program    {        static void Main(string[] args)        {            UserView _uM = new UserView();            Console.WriteLine("獲得hp : {0}" , _uM.Hp);            User uR = _uM.user;            uR.Hp = 89;            Console.WriteLine("改變獲得hp : {0}", _uM.Hp);            Console.WriteLine("-------------------------------------------------");            uR.IsLive = true;            Console.WriteLine("#################################################");            uR.IsLive = false;            Console.ReadLine();        }    }}

結果:

650) this.width=650;" title="01.png" alt="wKiom1lq7nqAi5gqAAAtsGl8Bng880.png-wh_50" src="https://s4.51cto.com/wyfs02/M01/9C/05/wKiom1lq7nqAi5gqAAAtsGl8Bng880.png-wh_500x0-wm_3-wmp_4-s_3950667275.png" />


本文出自 “Better_Power_Wisdom” 部落格,請務必保留此出處http://aonaufly.blog.51cto.com/3554853/1947974

C# 屬性綁定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.