0

これは少しばかげた質問かもしれませんが、次の問題に対する回避策や解決策を見つけることができませんでした...

  public class Example: IExample, INotifyPropertyChanged
  {
    public Example()
    {
    }

    /// Does not works properly... 
    string fooString;
    string IExample.A
    {
        get { return this.fooString; }
        set
        {
            this.fooString= value;
            onPropertyChange("A");
        }
    }

    /// Works just fine
    string fooString;
    public string A
    {
        get { return this.fooString; }
        set
        {
            this.fooString= value;
            onPropertyChange("A");
        }
    }

    PropertyChangedEventHandler propertyChangedHandler;

    private void onPropertyChange(string propertyName)
    {
        if (this.propertyChangedHandler != null)
            propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }

    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
        add { this.propertyChangedHandler += value; }
        remove { this.propertyChangedHandler -= value; }
    }

 }

コードからわかるように、INotifyPropertyChanged から実装するクラス Example と、1 つのプロパティ A を持つ IExample インターフェイスがあります。

Explicit インターフェイスの実装を使用しているため、 IExample インターフェイスを介してAを参照する必要があります。

そして、それが私の問題です。AIExamle から来ているため、値が変更されたときに明示的にINotifyPropertyChanged発生しません...

これは理にかなっています。

明示的なインターフェイスの実装と INotifyPropertyChanged を維持し、それでも仕事を成し遂げる方法についての考え/アイデアはありますか?

なぜ私がExplicit インターフェイスの実装に夢中になっているのかと疑問に思われるかもしれません

1) it's cool [I know horrible reason]
2) its clarity and better code readability [mostly because of this]
3) It forces you to use Interface   

ところで、 INotifyPropertyChanged 実装を自由に批判してください。これは、明示的な継承を処理できるようにするための方法です。

よろしくお願いします。

[編集] 「明示的な継承と明示的なインターフェイスの実装」を変更 - ダニエルのように修正、暗黙の継承コードを追加。明らかに、継承の1つをコメントアウトする必要があります...

4

1 に答える 1

1

コメントで言ったように、あなたが示していないコードで何か間違ったことをしているに違いありません。ここでイベントが発生します。

var example = new Example();
((INotifyPropertyChanged)example).PropertyChanged += OnAChanged;
((IExample)example).A = "new string";

http://ideone.com/jMpRG7を参照

于 2013-03-22T08:18:33.213 に答える