Application.Run 方法

定义

启动 Windows Presentation Foundation 应用程序。

重载

Run()

启动 Windows Presentation Foundation 应用程序。

Run(Window)

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

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 using 标记或标记和代码隐藏, Run 则将隐式调用。 但是,如果定义 Application 使用代码,则需要显式调用 Run

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

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

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

另请参阅

适用于

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

在应用程序启动时自动打开的 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 using 标记或标记和代码隐藏,则可以在使用以下任一技术期间自动打开窗口:

  • 以声明方式通过设置 StartupUri

  • 通过处理 以编程方式处理 Startup

另请参阅

适用于