C#屬性綁定

C#的反射明顯的比AS3的反射要強(qiáng)很多啊??梢詮耐獠吭L問到類的私有成員。舉個例子:

創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、靜安網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城系統(tǒng)網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為靜安等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

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

枚舉 : BindingFlags.NonPublic ->非公共成員將包括在內(nèi)進(jìn)行搜索,意思就是private成員也可以搜索到。

在model中注冊須發(fā)的主題(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 ? "活著" : "戰(zhàn)死");
        }

    }
}

好了 , 具體參數(shù)意思可以參考(《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();
        }
    }
}

結(jié)果:

C# 屬性綁定

附件:http://down.51cto.com/data/2366862

網(wǎng)站名稱:C#屬性綁定
分享網(wǎng)址:http://muchs.cn/article6/gjgsig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、微信小程序外貿(mào)建站、品牌網(wǎng)站制作全網(wǎng)營銷推廣、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作