2011-07-29 71 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属性。