如何在托管 C 中继承 ObservableCollection

发布于 2024-08-06 05:31:27 字数 504 浏览 1 评论 0原文

当我尝试在托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖个坑埋了你 2024-08-13 05:31:27

您是否确定添加了对 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.

人事已非 2024-08-13 05:31:27

我遇到了完全相同的问题; 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 没有。

<TargetFrameworkProfile>Client</TargetFrameworkProfile>

当指定“Client”时,DLL 来自:

C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client

当未指定“Client”时,DLL 来自:

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0

将“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.

<TargetFrameworkProfile>Client</TargetFrameworkProfile>

When "Client" is specified the DLL comes from:

C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client

When "Client" is not specified the DLL comes from:

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文