亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

INotifyPropertyChanged 和他的空事件

INotifyPropertyChanged 和他的空事件

C#
米脂 2022-12-24 12:26:24
我想用名稱“CurrentMoney”更新 WPF 表單上的標簽;我編寫了從“INotifyPropertyChanged”實現的類 Money。UPD:我通過創建 ViewModelBase 類更改為 MVVM 模式。仍然有“PropertyChaged”為空。如何解決它以及為什么會發生?Money.cs    public class Money : ViewModelBase {    private double currentMoney;    public double CurrentMoney {        get => currentMoney;        set {            currentMoney = value;            OnPropertyChanged("CurrentMoney");        }    }    public Money() => currentMoney = 10000;    public int addMoney(double count) {        CurrentMoney += count;        return 1;    }           public int subMoney(double count) {        CurrentMoney -= count;        if (currentMoney < 0)            return 100;        return 1;    }}主窗口.cspublic partial class MainWindow : Window {    public Money currentMoney;    public MainWindow ( ) {        InitializeComponent();        currentMoney = new Money();        CurrentMoney.DataContext = currentMoney;    }    private void Initialize() {        CurrentMoney.Content = "Current money: " + currentMoney.CurrentMoney;        CurrentPollution.Content = CurrentPollution.Content.ToString() + Pollution.CurrentPollution;        Facktories.Content = Facktories.Content.ToString() + FactoriesList.Quantity;        FactoriesPollution.Content = FactoriesPollution.Content.ToString() + FactoriesList.FullPolution;    }    private void MenuItem_Click(object sender, RoutedEventArgs e) {        var buy = new BuySMTH();        this.Visibility = Visibility.Collapsed;        buy.Show();    }    private void Window_ContentRendered(object sender, EventArgs e) {                   Initialize();    }    private void Tmp_Click(object sender, RoutedEventArgs e) {        currentMoney.addMoney(1000);    }
查看完整描述

3 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

您應該在屬性本身上實現接口。此外,在調用 PropertyChanged 事件時,您應該使用事件的本地(作用域)句柄以避免競爭條件。


public class Money : INotifyPropertyChanged {

    double _currentMoney;


    public event PropertyChangedEventHandler PropertyChanged;

    public double CurrentMoney 

    { 

         get => _currentMoney;

         set

         {

             _currentMoney = value;

             OnPropertyChanged();

         }

    }


    public Money() => CurrentMoney = 1000;


    public int addMoney(double count) {

        CurrentMoney += count;

        return 1;

    }       


    public int subMoney(double count) {

        CurrentMoney -= count;

        if (CurrentMoney < 0) { return 100; }

        return 1;

    }


    public void OnPropertyChanged([CallerMemberName] string propertyName = null) 

    {

        var handle = PropertyChanged;

        handle?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }


}

編輯:為了節省一些打字時間,您還可以創建一個 ViewModelBase 類來處理較小的細節。


public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable

{

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName] string propertyName = null) 

    {

        var handle = PropertyChanged;

        handle?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }

    public virtual void Dispose() => PropertyChanged = null;

}

然后在實現 viewModel 類時,只需繼承自 base(但仍會通知屬性更改)。


public class MyClass : ViewModelBase

{

    string _myField;

    public string MyProperty

    {

        get => _myField;

        set

        {

            _myField = value;

            OnPropertyChanged();

        } 

    }

}

注意:實際錯誤來自 [CallerMemberName] 屬性并從另一個方法中調用該方法。您可以傳入屬性名稱作為參數,也可以使用屬性本身的方法,而無需指定屬性名稱。


查看完整回答
反對 回復 2022-12-24
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

您需要傳遞修改后的道具名稱

NotifyPropertyChanged("CurrentMoney");


查看完整回答
反對 回復 2022-12-24
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

NotifyPropertyChanged 將 propertyName 作為參數。默認值為空。

private void NotifyPropertyChanged([CallerMemberName] string propertyName = null) {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

如果您想在 ui 上查看對“propertyName”的更改,可以使用 NotifyPropertyChanged("propertyName")。

對于您的示例,propertyName 是“CurrentMoney”


查看完整回答
反對 回復 2022-12-24
  • 3 回答
  • 0 關注
  • 196 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號