依存関係プロパティは、コンストラクターの変更通知や設定値
-
26-09-2019 - |
質問
I 3つの依存特性A、B、Cのクラスを有しています。これらのプロパティの値は、コンストラクタおよび特性A、B又はCの変更のたびに一つにより設定され、メソッドの再計算()が呼び出されます。 3つの特性A、B、Cが変更されるので、今コンストラクタの実行中に、これらの方法は、3回呼び出されます。 Hoeweverこれは、設定されたすべての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);
}
}
解決
あなたはこれを試すことができますか?
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);
}
他のヒント
使用DependencyObject.SetValueBase
。あなたのPropertyChangedが呼ばれることはありませんので、これは、任意のメタデータは、指定されたバイパス。 MSDN のを参照してください。
あなたはすべての3つのプロパティが設定されていない限り再計算を()を実行する必要はありません、しかし、それは、コンストラクタから呼び出されますか?これが正しいのですか?
もしそうなら、あなただけのすべての3つのプロパティが設定されていることを確認するために上部付近に再計算にチェックを入れて、決めることができないあなたが実行したいかどう....
あなたがと述べているので、これは動作します
メソッドの再計算などは、()で行うことはできません すべての3のない本当に便利なもの プロパティを設定します。