0

在我的应用程序中,我有 System.Windows(WindowsBase) 和 System.Windows.Forms 程序集。我必须使用 System.Windows.Application。

但是应用程序正在从 System.Windows.Forms 中获取 Application 类。

我指定了参考 System.Windows; 在代码中,但应用程序仍在从 System.Windows.Forms 中获取 Application 类。

您能帮我如何从 System.Windows 中引用“应用程序”类吗?

4

3 回答 3

1

使用完全限定名称,如下所示

System.Windows.Application app = new System.Windows.Application();
于 2013-10-08T20:31:57.157 回答
1

删除对 System.Windows.Forms 的引用,或者指定您在使用它时使用的内容。

IE

System.Windows.MessageBox mb = new System.Windows.MessageBox();
于 2013-10-08T20:31:57.380 回答
1

当你有相同的类名时,你需要用它们的命名空间前缀来完全限定它们中的至少一个。

您有可能将using指令添加到您的代码文件之一,如下所示:

using System.Windows.Forms;

如果这是一个 WPF 应用程序,您可能不希望将 using 指令添加到System.Windows.Forms. 如果您使用的是 Windows 窗体应用程序,则相反。

例如,假设您有以下代码:

using System.Windows;
using System.Windows.Forms;

...

Application myApp = new Application(); // this would throw a compile-time ambiguity error!

另一方面,您可以这样做:

using System.Windows;

...

Application myApp = new Application();
System.Windows.Forms.MessageBox.Show("test");
于 2013-10-08T20:37:42.087 回答