简体   繁体   English

接口的C#显式实现破坏了我的INotifyPropertyChanged

[英]C# Explicit implementation of interface breaks my INotifyPropertyChanged

This might be little bit stupid question but I could not find any work-around or think of any solutions to the following problem... 这可能是一个愚蠢的问题,但是我找不到任何解决方法,也没有想到以下问题的任何解决方案...

  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; }
    }

 }

As you can see from the code I have a class Example who implements from INotifyPropertyChanged and my IExample interface who has 1 property A. 从代码中可以看到,我有一个Example类,它通过INotifyPropertyChanged实现,我的IExample接口具有1个属性A。

Since I am using Explicit interface implementation I am forced to refer to my A through my IExample interface 由于我使用的是Explicit接口实现,因此我不得不通过IExample接口引用A

And that is where my problem is. 这就是我的问题所在。 Since A is coming from IExamle Explicitly when value changes INotifyPropertyChanged does not gets fired... 由于A明显来自IExamle,因此当值更改时INotifyPropertyChanged不会被触发...

Which makes sense. 有道理。

Any thoughts/ideas how to keep Explicit interface implementation and INotifyPropertyChanged and still get the job done? 有什么想法/想法如何保持Explicit接口实现和INotifyPropertyChanged并仍然完成工作?

You might ask why I am obsessed with Explicit interface implementation 您可能会问为什么我沉迷于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   

Btw Feel free to critique INotifyPropertyChanged implementation. 顺便说一句,随时批评INotifyPropertyChanged实现。 I feel I can re-implement this is a way so that it can handle Explicit inheritance 我觉得我可以重新实现这种方式,以便它可以处理显式继承

Thank you all in advance. 谢谢大家。

[Edit] Changed "explicit inheritance vs explicit interface implementation" - Like Daniel Corrected me, Added Implicit Inheritance code. [编辑]更改了“显式继承与显式接口实现”-像Daniel纠正了我一样,添加了隐式继承代码。 Obviously one of the inheritances should be commented out... 显然,其中一项继承应该被注释掉。

As I said in my comment, you must be doing something wrong in the code you didn't show us. 正如我在评论中说的那样,您必须在未向我们展示的代码中做错了什么。 Here the event is raised: 在这里引发事件:

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

see http://ideone.com/jMpRG7 参见http://ideone.com/jMpRG7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM