語言

Application.Run 方法

定義

啟動 Windows Presentation Foundation 應用程式。

多載

名稱 Description
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

Window當應用程式啟動時,它會自動開啟。

傳回

應用程式 Int32 關閉時會回傳給作業系統的退出程式碼。 根據預設,結束代碼值為0。

屬性

例外狀況

Run() 是從瀏覽器託管的應用程式呼叫(例如 XAML 瀏覽器應用程式(XBAP))。

範例

以下範例展示了一個應用程式,其手動建立靜態入口點方法,實例化 Application,然後呼叫 Run。

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。

當 Run 被呼叫時, Application 會將一個新 Dispatcher 實例附加到 UI 執行緒。 接著,呼叫物件 Dispatcher 的方法 Run ,啟動訊息泵浦來處理 Windows 訊息。 最後,物件 Dispatcher 呼叫該 Application 物件 OnStartup 的方法來引發事件 Startup 。 因此,應用程式執行模型在你處理 Startup時已經建立,該應用程式被視為正在執行。

應用程式在被呼叫時 Shutdown 停止執行;屬性的 ShutdownMode 值決定 Shutdown 何時被呼叫,以及是否自動執行,還是需要明確呼叫。

Run 只能從建立物件 Application 的執行緒中呼叫。 另外, Run XBAP 不能呼叫。

另請參閱

適用於