3

3 つの依存関係プロパティ A、B、C を持つクラスがあります。これらのプロパティの値はコンストラクターによって設定され、プロパティ A、B、または C のいずれかが変更されるたびに、メソッド recalculate() が呼び出されます。コンストラクターの実行中に、これらのメソッドが 3 回呼び出されます。これは、3 つのプロパティ A、B、C が変更されるためです。ただし、メソッド recalculate() は 3 つのプロパティがすべて設定されていないと、実際に役立つことは何もできないため、これは必要ありません。では、コンストラクターでこの変更通知を回避しながら、プロパティの変更通知を行う最善の方法は何でしょうか? コンストラクターにプロパティ変更通知を追加することを考えましたが、DPChangeSample クラスの各オブジェクトは常に変更通知をどんどん追加してしまいます。ヒントをありがとう!

class DPChangeSample : DependencyObject
{                  
    public static DependencyProperty AProperty = DependencyProperty.Register("A", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged));
    public static DependencyProperty BProperty = DependencyProperty.Register("B", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged));
    public static DependencyProperty CProperty = DependencyProperty.Register("C", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged));


    private static void propertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((DPChangeSample)d).recalculate();
    }


    private void recalculate()
    {
        // Using A, B, C do some cpu intensive calculations
    }


    public DPChangeSample(int a, int b, int c)
    {
        SetValue(AProperty, a);
        SetValue(BProperty, b);
        SetValue(CProperty, c);
    }
}
4

3 に答える 3

1

これを試してみませんか?

private bool SupressCalculation = false;
private void recalculate() 
{ 
    if(SupressCalculation)
        return;
    // Using A, B, C do some cpu intensive caluclations 
} 


public DPChangeSample(int a, int b, int c) 
{
    SupressCalculation = true; 
    SetValue(AProperty, a); 
    SetValue(BProperty, b); 
    SupressCalculation = false;
    SetValue(CProperty, c); 
} 
于 2010-06-12T14:24:20.177 に答える
1

を使用しDependencyObject.SetValueBaseます。これにより、指定されたメタデータがバイパスされるため、propertyChanged は呼び出されません。msdnを参照してください。

于 2010-06-12T12:51:50.930 に答える
0

3 つのプロパティがすべて設定されていない限り recalculate() を実行したくないのですが、a、b、c を設定するときにコンストラクタから呼び出されますか? これは正しいです?

もしそうなら、上部近くの再計算にチェックを入れて、3つのプロパティがすべて設定されていることを確認し、実行するかどうかを決定することはできませんか....

あなたがそれを言及しているので、これはうまくいきます

メソッド recalculate() は、3 つのプロパティがすべて設定されていないと、本当に役立つことは何もできないためです。

于 2010-06-12T12:59:08.277 に答える