1

Fody を使用して INotifyPropertyChanged をプロパティに挿入する Windows Phone 8 アプリケーションがあります。ビューのテキストボックスにバインドされているプロパティ A を持つ Class First があります。

[ImplementPropertyChanged]
public class First
{
    public int A { get; set; }

    public int AA { get {return A + 1; } }
}

また、プロパティ A に応じてプロパティ B を持つクラス Second (テキストボックスにもバインドされています):

[ImplementPropertyChanged]
public class Second
{
    private First first;

    public int B { get {return first.A + 1; } }
}

A と AA の更新は正常に機能しますが、B は最初に A が変更されたときに自動的に更新されません。fody を使用してこのような自動更新を実現する簡単でクリーンな方法はありますか?それを処理するために独自のイベントを作成する必要がありますか?

4

2 に答える 2

1

SKallが提案した方法で標準の INotifyPropertyChanged を使用することになりました。

public class First : INotifyPropertyChanged
{
    public int A { get; set; }

    public int AA { get {return A + 1; } }

    (...) // INotifyPropertyChanged implementation
}

public class Second : INotifyPropertyChanged
{
    private First first;

    public Second(First first)
    {
        this.first = first;
        this.first.PropertyChanged += (s,e) => { FirstPropertyChanged(e.PropertyName);

        public int B { get {return first.A + 1; } }

        protected virtual void FirstPropertyChanged(string propertyName)
        {
            if (propertyName == "A")
                NotifyPropertyChanged("B");
        }

        (...) // INotifyPropertyChanged implementation
    }
};
于 2014-02-18T10:35:44.393 に答える
1

私は Fody に詳しくありませんが、Second.B にセッターがないためだと思います。Second は First の変更をサブスクライブする必要があり、First.A が変更されるプロパティである場合は、B の (プライベート) セッターを使用する必要があります。

または、First にサブスクライブしてから、B プロパティ変更イベントを呼び出します。

[ImplementPropertyChanged]
public class Second
{
    private First first;

    public int B { get {return first.A + 1; } }

    public Second(First first)
    {
        this.first = first;
        this.first.OnPropertyChanged += (s,e) =>
        {
            if (e.PropertyName == "A") this.OnPropertyChanged("B");
        }
}
于 2014-01-06T00:10:59.213 に答える