2013-10-08 32 views
0

在我的應用程序中,我有兩個System.Windows(WindowsBase)和System.Windows.Forms程序集。我必須使用System.Windows.Application。System.windows和system.windows.forms

但是應用程序正在從System.Windows.Forms中取出Application類。

我指定參考System.Windows;在代碼中,但應用程序仍然從System.Windows.Forms中獲取Application類。

你能幫我一下如何從System.Windows中引用「應用程序」類。

+0

你到底想幹什麼?您的應用程序是Winforms應用程序還是WPF應用程序?這意味着定義了「Main」方法的初始對象是什麼? 'System.Windows.Application.Current'將在Winforms應用中爲空。 –

回答

1

使用完全合格的名稱像下面

System.Windows.Application app = new System.Windows.Application(); 
1

刪除提及System.Windows.Forms的,要不指定當你使用它,你正在使用。

System.Windows.MessageBox mb = new System.Windows.MessageBox(); 
1

當你有你需要的完全限定它們中的至少一個與他們的命名空間前綴相同的類名。

機會是你增加了一個using指令代碼文件中的一個類似如下:

using System.Windows.Forms; 

如果這是你可能不希望永遠的使用指令添加到System.Windows.Forms WPF應用程序。如果您使用的是Windows Forms應用程序,則情況相反。

例如,假設你有下面的代碼:

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"); 
+0

謝謝大家回覆。根據您的建議,我嘗試使用完全限定的名稱。但我沒有找到System.Windows下的「應用程序」。看着WindowsBase程序集,我沒有看到System.Windows.Application。我可以找到哪個庫/程序集?我認爲這是WindowsBase。 – user12121

+0

Google是完全限定的類名「System.Windows.Application」,MSDN參考頁將告訴您程序集DLL的名稱。 –

+0

謝謝。我能夠解決這個問題。我使用錯誤的DLL。現在,當我試圖從下面的代碼中獲取「Stream」時:「Stream stream = Application.GetResourceStream(uri).Stream;」 ..獲取以下錯誤信息。這個錯誤的含義是什麼「錯誤類型'System.Windows.Markup.IQueryAmbient'在未引用的程序集中定義,必須添加對程序集」System.Xaml,Version = 4.0.0.0,Culture =中立,PublicKeyToken = b77a5c561934e089'\t test.cs TestSolution 「 – user12121

相關問題