通过


Application.Run 方法

定义

启动 Windows Presentation Foundation 应用程序。

重载

名称 说明
Run(Window)

启动 Windows Presentation Foundation 应用程序并打开指定的窗口。

Run()

启动 Windows Presentation Foundation 应用程序。

Run(Window)

启动 Windows Presentation Foundation 应用程序并打开指定的窗口。

public:
 int Run(System::Windows::Window ^ window);
[System.Security.SecurityCritical]
public int Run(System.Windows.Window window);
public int Run(System.Windows.Window window);
[<System.Security.SecurityCritical>]
member this.Run : System.Windows.Window -> int
member this.Run : System.Windows.Window -> int
Public Function Run (window As Window) As Integer

参数

window
Window

应用程序启动时自动打开的 A Window

返回

Int32应用程序关闭时返回到操作系统的应用程序退出代码。 默认情况下,退出代码值为 0。

属性

例外

Run() 是从浏览器托管的应用程序(例如 XAML 浏览器应用程序(XBAP)调用的。

示例

以下示例演示了一个应用程序,其中包含在调用Run之前实例化Application手动创建的静态入口点方法。

using System;
using System.Windows;

namespace CSharp
{
    public class EntryPoint
    {
        // All WPF applications should execute on a single-threaded apartment (STA) thread
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new Window());
        }
    }
}

Imports System.Windows

Namespace VisualBasic
    Public Class EntryPoint
        ' All WPF applications should execute on a single-threaded apartment (STA) thread
        <STAThread()>
              Public Shared Sub Main()
            Dim app As New Application()
            app.Run(New Window())
        End Sub
    End Class
End Namespace

注解

此重载扩展 Run 了在应用程序开始运行后打开指定窗口的方法。

如果定义在开始运行时打开窗口的代码 Application ,则显式调用 Run

如果使用标记或标记和代码隐藏创建 Application ,则可以使用以下任一技术自动打开窗口:

另请参阅

适用于

Run()

启动 Windows Presentation Foundation 应用程序。

public:
 int Run();
public int Run();
member this.Run : unit -> int
Public Function Run () As Integer

返回

Int32应用程序关闭时返回到操作系统的应用程序退出代码。 默认情况下,退出代码值为 0。

例外

Run() 是从浏览器托管的应用程序(例如 XAML 浏览器应用程序(XBAP)调用的。

示例

以下示例演示使用自定义 Application 的应用程序,因此必须显式调用 Run

using System;
using System.Windows;

namespace CSharp
{
    public class EntryPoint1
    {
        // All WPF applications should execute on a single-threaded apartment (STA) thread
        [STAThread]
        public static void Main()
        {
            CustomApplication app = new CustomApplication();
            app.Run();
        }
    }

    public class CustomApplication : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Window window = new Window();
            window.Show();
        }
    }
}

Imports System.Windows

Namespace VisualBasic
    Public Class EntryPoint
        ' All WPF applications should execute on a single-threaded apartment (STA) thread
        <STAThread()>
              Public Shared Sub Main()
            Dim app As New CustomApplication()
            app.Run()
        End Sub
    End Class

    Public Class CustomApplication
        Inherits Application
        Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)
            MyBase.OnStartup(e)

            Dim window As New Window()
            window.Show()
        End Sub
    End Class
End Namespace

注解

Run 调用 以启动 WPF 应用程序。 如果定义 Application 使用标记或标记和代码隐藏, Run 将隐式调用。 但是,如果定义 Application 使用代码,则需要显式调用 Run

调用时 RunApplication 将新 Dispatcher 实例附加到 UI 线程。 接下来, Dispatcher 调用对象的 Run 方法,该方法启动消息泵来处理 Windows 消息。 最后,该Dispatcher对象调用对象的ApplicationOnStartup方法以引发Startup事件。 因此,在处理 Startup应用程序时已建立应用程序执行模型,此时应用程序被视为正在运行。

调用应用程序时 Shutdown 停止运行;属性的值 ShutdownMode 确定 Shutdown 何时被调用,以及它是自动发生的还是需要显式调用它。

Run 只能从创建 Application 对象的线程调用。 此外, Run 不能从 XBAP 调用。

另请参阅

适用于