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 使用標記或標記和程式碼後置, 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 ,您可以使用下列任一技術自動開啟視窗:

  • 以宣告方式,方法是設定 StartupUri

  • 以程式設計方式處理 Startup

另請參閱

適用於