.net 使用C++/Windows窗体应用程序中的CLI

.net 使用C++/Windows窗体应用程序中的CLI,.net,winforms,visual-studio-2010,c++-cli,.net,Winforms,Visual Studio 2010,C++ Cli,我试图通过在Windows窗体项目中利用使用名称空间来节省一些编码时间。我在VS2010中使用C++/CLI创建了一个默认的Windows窗体项目。我注意到导入的默认名称空间是: using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace Sys

我试图通过在Windows窗体项目中利用
使用名称空间
来节省一些编码时间。我在VS2010中使用C++/CLI创建了一个默认的Windows窗体项目。我注意到导入的默认名称空间是:

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
我想创建一个
DialogResult
类型的变量,它(非常方便!)位于
System::Windows::Forms
名称空间中。我进入默认
表单1的构造函数并添加行:

DialogResult dr;
我得到编译器错误
语法错误:缺少“;”在标识符“dr”之前

但是,如果我将行更改为:

Windows::Forms::DialogResult dr;

然后一切按预期进行

我还尝试添加

using namespace System::Windows;
然后

Forms::DialogResult dr
也行


关于这些名称空间的工作方式,我遗漏了什么?!我希望避免必须完全限定我正在编写的所有代码,但我无法找出我做错了什么,因为我需要的命名空间应该已经导入了。

System::Windows::Forms::Form
有一个名为
DialogResult
的属性,因此在
表单
子类中,该属性的作用域优先于全局命名空间中的类型

我通常使用typedef解决这个问题:

typedef System::Windows::Forms::DialogResult DialogResult_t;
然后,在需要使用该类型的任何时候,都可以使用
DialogResult\t
,在需要访问该属性的任何时候,都可以使用
DialogResult


注意,这个问题不是C++/CLI特定的——C++具有相同的范围规则,因此会有相同的问题;只是.NETBCL将类型名称重用为非常广泛的属性名称(C++代码会避免),因为C ^没有这个问题。我本应该弄明白这一点,但伟大的解释和有益的建议!谢谢!

typedef System::Windows::Forms::DialogResult DialogResult_t;