当另一个类中的先决条件属性发生更改时,为依赖属性引发 PropertyChanged? [英] Raising PropertyChanged for a dependent property, when a prerequisite property is changed in another class?

查看:23
本文介绍了当另一个类中的先决条件属性发生更改时,为依赖属性引发 PropertyChanged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Bank 类:

public class Bank : INotifyPropertyChanged
{
    public Bank(Account account1, Account account2)
    {
        Account1 = account1;
        Account2 = account2;
    }

    public Account Account1 { get; }
    public Account Account2 { get; }

    public int Total => Account1.Balance + Account2.Balance;

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Bank 依赖于其他类,并且有一个属性 Total 是根据这些其他类的属性计算出来的.每当这些 Account.Balance 属性中的任何一个发生更改时,都会为 Account.Balance 引发 PropertyChanged:

Bank depends on other classes and has a property Total that is calculated from properties of these other classes. Whenever any of these Account.Balance properties is changed, PropertyChanged is raised for Account.Balance:

public class Account : INotifyPropertyChanged
{
    private int _balance;

    public int Balance
    {
        get { return _balance; }
        set
        {
            _balance = value;
            RaisePropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

我想为 Total 提高 PropertyChanged,只要任何先决条件属性发生更改.我怎样才能以一种易于测试的方式做到这一点?

I would like to raise PropertyChanged for Total, whenever any of the prerequisite properties is changed. How can I do this in a way that is easily testable?

TL;DR 当另一个类中的先决条件属性发生更改时,如何为依赖属性提升 PropertyChanged?

TL;DR How do I raise PropertyChanged for a dependent property, when a prerequisite property is changed in another class?

推荐答案

您可以通过多种不同的方式来做到这一点.我见过许多不同的解决方案,它们在单个属性 setter 中调用自定义属性或引发多个 PropertyChanged 事件.我认为大多数这些灵魂都是反模式的,不容易测试.

You can do this in many different ways. I have seen many different solutions, that invovle custom attributes or raising multiple PropertyChanged events in a single property setter. I think most of these soultions are anti-patterns, and not easily testable.

我和同事 (Robert Jørgensgaard Engdahl) 的最佳方式这是静态类:

The best way a colleague (Robert Jørgensgaard Engdahl) and I have come up with is this static class:

public static class PropertyChangedPropagator
{
    public static PropertyChangedEventHandler Create(string sourcePropertyName, string dependantPropertyName, Action<string> raisePropertyChanged)
    {
        var infiniteRecursionDetected = false;
        return (sender, args) =>
        {
            try
            {
                if (args.PropertyName != sourcePropertyName) return;
                if (infiniteRecursionDetected)
                {
                    throw new InvalidOperationException("Infinite recursion detected");
                }
                infiniteRecursionDetected = true;
                raisePropertyChanged(dependantPropertyName);
            }
            finally
            {
                infiniteRecursionDetected = false;
            }
        };
    }
}

它创建了一个 PropertyChangedEventHandler,您可以将其设置为侦听其他类上的 PropertyChanged.在抛出 StackOverflowException 之前,它使用 InvalidOperationException 处理循环依赖.

It creates a PropertyChangedEventHandler, which you can set up to listen on PropertyChanged on other classes. It handles circular dependencies with an InvalidOperationException before an StackOverflowException is thrown.

要在上面的示例中使用静态 PropertyChangedPropagator,您必须为每个先决条件属性添加一行代码:

To use the static PropertyChangedPropagator in the example from above, you will have to add one line of code for each prerequisite property:

public class Bank : INotifyPropertyChanged
{
    public Bank(Account account1, Account account2)
    {
        Account1 = account1;
        Account2 = account2;
        Account1.PropertyChanged += PropertyChangedPropagator.Create(nameof(Account.Balance), nameof(Total), RaisePropertyChanged);
        Account2.PropertyChanged += PropertyChangedPropagator.Create(nameof(Account.Balance), nameof(Total), RaisePropertyChanged);
    }

    public Account Account1 { get; }
    public Account Account2 { get; }

    public int Total => Account1.Balance + Account2.Balance;


    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

这很容易测试(伪代码):

This is easily testable (pseudo code):

[Test]
public void Total_PropertyChanged_Is_Raised_When_Account1_Balance_Is_Changed()
{
    var bank = new Bank(new Account(), new Account());

    bank.Account1.Balance += 10;

    Assert.PropertyChanged(bank, nameof(Bank.Total));
}

这篇关于当另一个类中的先决条件属性发生更改时,为依赖属性引发 PropertyChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆