如何在托管 C 中继承 ObservableCollection
当我尝试在托管 C++ 中创建一个继承自 ObservableCollection 的类时,出现错误: 错误 C2039:“ObservableCollection”:不是“System::Collections::ObjectModel”的成员
这是我的代码:
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
public ref class DataMatrix : public System::Collections::ObjectModel::ObservableCollection<array<Object^>^> {};
为什么我不能从 C++-CLI 使用此类?我在 C# 中使用它没有任何困难。
When I try to create a class in managed C++ that inherits from ObservableCollection I get the error:
error C2039: 'ObservableCollection' : is not a member of 'System::Collections::ObjectModel'
Here's my code:
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
public ref class DataMatrix : public System::Collections::ObjectModel::ObservableCollection<array<Object^>^> {};
Why can't I use this class from C++-CLI? I have no difficulty using it in C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否确定添加了对 WindowsBase.dll 的引用?
ObservableCollection
位于此 DLL 中,并且不包含在 C++ 项目的默认引用列表中。Did you make sure to add a reference to WindowsBase.dll?
ObservableCollection<T>
lives in this DLL and it is not included on the default references list for a C++ project.我遇到了完全相同的问题; VS2010。我引用了 WindowsBase.dll,但仍然收到错误。我在使用 ObservableCollection 的同一解决方案中有一个 C# 项目,它编译得很好。我最终发现这与我将目标.NET框架设置为V3.5有关(MMC项目和MMC尚不支持.NET 4.0)。我已将 C# 项目设置为使用“.NET V3.5 客户端”,但托管 C++ 项目只是设置为“.NET V3.5”。看来 ObservableCollection 定义可以在 WindowsBase.dll 的“客户端”版本中找到,但不能在常规版本中找到。
换句话说,.csproj 文件包含以下行,但 .vcproj 没有。
当指定“Client”时,DLL 来自:
当未指定“Client”时,DLL 来自:
将“TargetFrameworkProfile”标记添加到 .vcproj 强制编译器使用 WindowsBase.dll 的客户端版本,然后编译将成功。我无法解释为什么,但我很高兴把这个令人头疼的问题抛在脑后。
I had exactly the same problem; VS2010. I had a reference to WindowsBase.dll but I still got the error. I have a C# project in the same solution that uses ObservableCollection and it compiles fine. I eventually figured out that it was related to the fact that I had set the targeted .NET framework to V3.5 (MMC project and MMC does not yet support .NET 4.0). I had set the C# project to use ".NET V3.5 Client" but the managed C++ project was simply set to ".NET V3.5". It seems that the ObservableCollection definition could be found in the "client" version of the WindowsBase.dll but not in the regular version.
Stating things in a different way, the .csproj file contained the following line but the .vcproj did not.
When "Client" is specified the DLL comes from:
When "Client" is not specified the DLL comes from:
Adding the "TargetFrameworkProfile" tag to the .vcproj forced the compiler to use the client version of WindowsBase.dll and then the compile would succeed. I can't explain why, but I'm glad to put this head scratcher behind me.