第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

冒泡 NotifyPropertyChanged

冒泡 NotifyPropertyChanged

C#
不負(fù)相思意 2021-11-07 19:20:58
根據(jù)這個(gè)答案,我不需要擔(dān)心 NotifyPropertyChanges 冒泡層次結(jié)構(gòu),但我仍然無法使用這樣的(簡(jiǎn)化的測(cè)試-)結(jié)構(gòu):數(shù)據(jù)保持類public class TestNotifyChanged : INotifyPropertyChanged{    public event PropertyChangedEventHandler PropertyChanged;    private string _Test = "default";    public string Test    {        get        {            return _Test;        }        set        {            if(_Test!=value)            {                _Test = value;                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Test"));            }        }    }}使用該測(cè)試類和測(cè)試屬性的 ViewModel:public class ViewModel : INotifyPropertyChanged{    public event PropertyChangedEventHandler PropertyChanged;    private TestNotifyChanged tnc = new TestNotifyChanged(); // only to init, otherwise VS screams at me    public ViewModel(TestNotifyChanged tnc)    {        tnc = tnc; // getting an instance of TestNotifyChanged from "Master" passed in, which hopefully will be replaces by a singleton class.    }    private string _Test;    public string Test    {        get        {            return tnc.Test;  // this might be the crucial part!?        }        set        {            if (_Test != value) // never hits that, as I would expect, but you never know..            {                _Test = value;                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Test"));  // of course also never hit, as expected            }        }    }}最后是我的 MainWindow cspublic partial class MainWindow : Window{    TestNotifyChanged tnc;    public MainWindow()    {        InitializeComponent();        tnc = new TestNotifyChanged();        DataContext = new ViewModel(tnc); // pass in my Test-Object that has the Values.    }現(xiàn)在發(fā)生了什么:默認(rèn)值“default”顯示在 TextBlock 中。當(dāng)我單擊按鈕時(shí),消息框顯示“新值”TextBlock未更新為“新值”我想要達(dá)到的目標(biāo):看起來很簡(jiǎn)單:TextBlock 應(yīng)該更新為“新值”當(dāng)我直接在 ViewModel 上設(shè)置測(cè)試值時(shí),我可以輕松地完成這項(xiàng)工作- 但這似乎不正確,并且與我認(rèn)為我可以構(gòu)建我的應(yīng)用程序/代碼的方式相去甚遠(yuǎn)。未來的目標(biāo)是擁有一個(gè)單例(我發(fā)現(xiàn)靜態(tài)不起作用)“RecordStore”,它包含大部分?jǐn)?shù)據(jù)(并從 API、本地?cái)?shù)據(jù)庫(kù)或僅從內(nèi)存中獲取它,如果其中任何一個(gè)完成)所以問題是:為什么 NotifyPropertyChange 沒有冒泡到 View/ViewModel?或者還有其他我沒有看到的問題?
查看完整描述

1 回答

?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊

“冒泡”是路由事件的概念。像 PropertyChanged 這樣的常規(guī)事件不會(huì)“冒泡”。


除了tnc = tnc;ViewModel 中的明顯錯(cuò)誤(應(yīng)該是this.tnc = tnc;)之外,這兩個(gè)類的 Test 屬性是不相關(guān)的。為了更新自己的 Test 屬性,ViewModel 必須在 處注冊(cè)一個(gè) PropertyChanged 事件處理程序tnc。并且它必須tnc在其自身的 Test 屬性更改時(shí)更新該屬性。


public class ViewModel : INotifyPropertyChanged

{

    public event PropertyChangedEventHandler PropertyChanged;


    private TestNotifyChanged tnc;


    public ViewModel(TestNotifyChanged t)

    {

        tnc = t;

        tnc.PropertyChanged += (s, e) =>

        {

            if (e.PropertyName == nameof(Test) || string.IsNullOrEmpty(e.PropertyName))

            {

                Test = tnc.Test; // update ViewModel.Test from TestNotifyChanged.Test

            }

        };

    }


    private string test;

    public string Test

    {

        get

        {

            return test; // always return own value

        }

        set

        {

            if (test != value)

            {

                test = value;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Test)));


                tnc.Test = Test; // update TestNotifyChanged.Test from ViewModel.Test

            }

        }

    }

}

或者,刪除 Test 屬性的支持字段并僅操作tnc.Test:


public class ViewModel : INotifyPropertyChanged

{

    public event PropertyChangedEventHandler PropertyChanged;


    private TestNotifyChanged tnc;


    public ViewModel(TestNotifyChanged t)

    {

        tnc = t;

        tnc.PropertyChanged += (s, e) =>

        {

            if (e.PropertyName == nameof(Test) || string.IsNullOrEmpty(e.PropertyName))

            {

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Test)));

            }

        };

    }


    public string Test

    {

        get { return tnc.Test; }

        set { tnc.Test = Test; }

    }

}

幸運(yùn)的是,這完全沒有必要。


相反,可能只是一個(gè)公共Tnc財(cái)產(chǎn),如


public class ViewModel

{

    public TestNotifyChanged Tnc { get; }


    public ViewModel(TestNotifyChanged tnc)

    {

        Tnc = tnc;

    }

}

使用這樣的綁定:


<TextBlock Text="{Binding Tnc.Test}"/>


查看完整回答
反對(duì) 回復(fù) 2021-11-07
  • 1 回答
  • 0 關(guān)注
  • 423 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)