2011-07-29 70 views
-1

我想獲得寫入C#的應用程序的主窗口。 Application.MainWindow屬性不工作:(獲取主窗口

用途:?

using System; 
using System.Windows; 

using System.Windows.Forms; 
using System.ComponentModel; 
using System.Drawing; 
using System.Collections.Generic; 

using My; 
using MyDialogs; 

using System.Xml.Serialization; 
using System.Runtime.InteropServices; 

using System.Windows.Input; 
using System.Windows.Media; 
using System.Threading; 
using System.Windows.Interop; 
+2

不以什麼方式工作? –

+0

你試着在WPF或Winforms?如果它是winform檢查這個URL http://social.msdn.microsoft.com/Forums/en/winforms/thread/99df9c07-c117-465a-9207-fa3534982021 –

+0

它不作爲屬性,Window對象存在。 – jahsen

回答

0

你有一行這樣的代碼在應用程序中的任何地方

Application.Run(new Form1()); 

凡Form1的是類型當你創建一個新的Windows窗體應用程序時,這是默認創建的代碼,如果你想記住這個實例,你只需要將結果存儲在其他類可以訪問的變量中。 e:

static class Program 
{ 
    public static Form1 MainForm; 

    // ... 

    static void Main() 
    { 
    // ... 
    MainForm = new Form1(); 
    Application.Run(MainForm); 
    } 
} 
+0

我有這樣的私人靜態無效的主要(字符串[]參數) \t \t { \t \t \t Application.EnableVisualStyles(); \t \t \t Application.SetCompatibleTextRenderingDefault(false); \t \t \t Application.Run(new MainForm()); \t \t} – jahsen

+0

好吧,但我需要窗口對象不形式 – jahsen

+0

我明白了:)謝謝! – jahsen

1

我認爲你的應用程序類型是Windows Forms應用程序。這是繼從您發佈:

我有這個

private static void Main(string[] args) { 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new MainForm()); 
} 

所以,你不能使用主窗口對象(類型System.Windows.Window的),因爲它使用WPF。創建新的WPF項目,並且您可以訪問Application.MainWindow屬性。