0

我正在尝试在 C++/CLI 中实现一个抽象的 C# 类。这个抽象基类已经在实现 INotifyPropertyChanged 并且如用 C# 编写的那样:

public abstract class BaseClass : INotifyPropertyChanged

在 C++/CLI 程序集中,我确实有另一个实现 INotifyPropertyChanged 的​​接口:

public interface class IAnotherNotifyPropertyChangedClass : public INotifyPropertyChanged

现在,当从抽象 C# 类 BaseClass 继承并在 C++/CLI 中实现 IAnotherNotifyPropertyChangedClass 时,我得到以下信息:

public ref class AnotherNotifyPropertyChangedClass : public BaseClass, public IAnotherNotifyPropertyChangedClass

这会导致以下编译错误:

error C3766: 'AnotherNotifyPropertyChangedClass' must provide an implementation for the interface method 'void System::ComponentModel::INotifyPropertyChanged::PropertyChanged::add(System::ComponentModel::PropertyChangedEventHandler ^)'

一旦我从 IAnotherNotifyPropertyChangedClass 接口声明中删除 INotifyPropertyChanged,一切都编译得很好。这是为什么?使用 C# 时,此声明可以正常编译。我正在使用 VS 2012 并编译一个 .NET 3.5 混合模式程序集。

提前致谢!

干杯!

编辑:类似的问题(没有 C#)在这里:http ://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/3047b8d1-348c-4ca6-b3f3-c396c03fedf7/ C++ 中的这种行为也是如此/CLI 设计的?!

4

3 回答 3

3

这是不正常的,我没有得到重现。这个问题没有显示实际的代码,我会展示我的:

C# 测试类:

using System.ComponentModel;

namespace ClassLibrary8 {
    public abstract class CFoo : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

将引用添加到 C# 项目后的 C++/CLI 测试代码:

using namespace System::ComponentModel;

public interface class IBar : public INotifyPropertyChanged {
};

public ref class Baz : ClassLibrary8::CFoo, IBar {
    // fine
};
于 2013-05-15T21:47:57.883 回答
2

您需要做的是在您的 C++/CLI 类中明确实现 INotifyPropertyChanged。您可以让它调用已经实现的 C# 版本。

在您的情况下,我不能 100% 确定,您可能必须明确实施AnotherNotifyPropertyChangedClass::PropertyChanged而不是INotifyPropertyChanged::PropertyChanged.

private:
    event PropertyChangedEventHandler^ DuplicatePropertyChanged
    {
        virtual void add (PropertyChangedEventHandler^ value) sealed = 
            INotifyPropertyChanged::PropertyChanged::add
        {
            // Add to the event defined in the C# class.
            this->PropertyChanged += value;
        }

        virtual void remove (PropertyChangedEventHandler^ value) sealed = 
            INotifyPropertyChanged::PropertyChanged::remove
        {
            // Remove from the event defined in the C# class.
            this->PropertyChanged -= value;
        }
    }
于 2013-05-15T21:46:34.523 回答
1

那是因为interafce需要一个像这样的实现:

#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion

因为 C# 类是抽象的,所以它不需要(必须)处理它,您需要在 C++ 类中实现它。那是第一个不是抽象的类。

于 2013-05-15T21:07:55.710 回答