Wpf

WPFのApplicationクラス。



Application Class Wpf



元の: WPFのApplicationクラス。

Applicationオブジェクトが使用する名前空間はsystem.windowsです。

1.アプリケーションオブジェクトステップを手動で作成します。

1.1)プロジェクト内のApp.Xamlファイルをプロジェクトから除外します。



1.2)、新しいスタートアップクラスを作成します。

1.3)クラスに戻り値のない静的Mainメソッドを宣言します。



1.4)メソッドにアプリケーションオブジェクトとウィンドウオブジェクトを作成します。

public class Startup { [STAThread] //The COM thread mode of the specified application is a single-threaded apartment (STA) static void Main() { //method 1: //Create an Application object. Application app = new Application() //Instantiate the window. MainWindow mw = new MainWindow() //When a window is passed to the Application.Run() method, the window is set to the main window, which can be accessed throughout the application via the Application.MainWindow property. Then use the Run() method to trigger the Application.Startup event to display the main window.  app.Run(mw) //Method 2: //Create an Application object. Application app = new Application() //Instantiate the window. MainWindow mw = new MainWindow() //Set the application main window. app.MainWindow = mw //pop-up window  mw.Show() //When a window is passed to the Application.Run() method, the window is set to the main window, which can be accessed throughout the application via the Application.MainWindow property. Then use the Run() method to trigger the Application.Startup event to display the main window.  app.Run() //Method 3 //Create an application object. Application app = new Application() //Set uri. app.StartupUri = new Uri('MainWindow.xaml', UriKind.Relative) app.Run() } }
コードを見る

2.アプリケーションのシャットダウンモードを設定します。

Applicationクラスは、ウィンドウが閉じられていない限り、アプリケーションを有効な状態に保ちます。これが予期された動作でない場合は、Application.ShutdownModeプロパティを調整します。 Applicationオブジェクトを手動で作成する場合は、Run()メソッドを呼び出す前にShutDownModeプロパティを設定する必要があります。 App.Xamlファイルを使用している場合は、xamlファイルでShutdownModeプロパティを個別に設定できます。Application.Run()メソッドが実行されると、Application.Run()メソッドはすぐに戻ります。

ShutdownMode列挙値



名前

説明

OnLastWindowClose

デフォルトの動作では、少なくとも1つの他のウィンドウが存在する限り、アプリケーションは実行されたままになります。

OnMainWindowClose

従来、メインウィンドウが開いている限り、アプリケーションは実行されたままになります。

OnExceptionShutdown

Application.Shutdown()メソッドが呼び出されない限り、アプリケーションは終了しません(すべてのプログラムが閉じられている場合でも)。

3.一般的なアプリケーションイベント。

名前

説明

起動

このイベントは、Application.Run()メソッドが呼び出された後、メインウィンドウが表示される前に実行されます。

出口

このイベントは、アプリケーションが(何らかの理由で)閉じられたときに発生し、Runメソッドが戻る直前、Run()メソッドの前に発生します。

SessionEnding

このイベントは、Windowsの会話の最後に発生します

有効化

このイベントは、アプリケーションのウィンドウがアクティブ化されたときに発生し、別のウィンドウプログラムに切り替えたときにもトリガーされます。

非アクティブ化

このイベントは、アプリケーションのウィンドウが非アクティブ化されたときに発生し、別のウィンドウプログラムに切り替えたときにもトリガーされます。

DispatcherUnhandledException

未処理の例外が発生すると、アプリケーションはこのイベントに入ります。 Handledプロパティをtrueに設定して、アプリケーションの実行を続行することもできます。

4.初期インターフェースを表示します。

WPFアプリケーションは高速ですが、すぐには起動しません。初めてアプリケーションを起動するときは、多少の遅延があります。共通言語が実行されるため、最初に.Net環境を初期化してから、アプリケーションを起動する必要があります。今回は初期表示が表示されます。インターフェイスには役割があります。 Runメソッドの前に次のコードを実行します。

//The background image when the screen is initialized. SplashScreen splashScreen = new SplashScreen('1.png') //The initial screen is displayed. splashScreen.Show(true) //Fade out the initial interface time (in seconds). splashScreen.Close(TimeSpan.FromSeconds(1))

5.現在のアプリケーションオブジェクトにアクセスします。

現在のアプリケーションインスタンスは、Application.Current.MainWindowプロパティを介してアプリケーションのどこからでも取得できます。これにより、複数のウィンドウ間の基本的な相互作用が可能になります。

MainWindowにTest()メソッドがあるとすると、次の方法でWindow1ウィンドウからアクセスできます。

//Get the main window object. MainWindowainWindow mw = (MainWindow)Application.Current.MainWindow //Call the main window Test() method. mw.Test()

6.ウィンドウ間のインタラクティブな手順。

6.1)App.xamlファイルを削除します。

6.2)、新しいAppクラスを作成し、Applicationから継承し、ウィンドウ起動アイテムを内部に設定し、ウィンドウタイプのコレクションを書き込みます。

6.3)子ウィンドウがメインウィンドウでインスタンス化されたら、Owerプロパティをメインウィンドウと同じ子ウィンドウに設定します。

6.4)子ウィンドウで、Application.Current.MainWindowプロパティを介してメインウィンドウオブジェクトを取得し、メソッドを呼び出します。

6.5)メインウィンドウのコレクション内のデータをループして、サブウィンドウを設定します。

アプリクラス:

public class App : Application { [STAThread] public static void Main() { //Instantiate a class. App app = new App() app.InitializeComponent() app.Run() } private void InitializeComponent() { //Set the main window startup item. this.StartupUri = new Uri('MainWindow.xaml', UriKind.Relative) } //Declare a collection of Window types for placing child windows. private List listWindow = new List() public List ListWindow { get { return listWindow } set { this.listWindow = value } } }

MainWinowバックグラウンドコード:

public partial class MainWindow : Window { public MainWindow() { InitializeComponent() this.Loaded += MainWindow_Loaded } void MainWindow_Loaded(object sender, RoutedEventArgs e) { //The Winow1 window pops up when the window is loaded. Window1 w1 = new Window1() //Is the Window1Owner property. w1.Owner = this //show.  w1.Show() //Add Winow1 to the collection.  ((App)App.Current).ListWindow.Add(w1) } ///  /// This method is used to set the main window title to the current time. ///  public void SetMainWinowTime() { this.Title = DateTime.Now.ToString() } ///  /// This method is used to set the sub-window title to the current time. ///  ///  ///  private void Button_Click(object sender, RoutedEventArgs e) { for (int i = 0 i <((App)App.Current).ListWindow.Count i++) { //Converts the orientation in the collection to a Window1 object and then calls the SetWindowTime() method.  ((Window1)((App)App.Current).ListWindow[i]).SetWindowTime() } }  } 

ウィンドウウィンドウの背景コード:

public partial class Window1 : Window { public Window1() { InitializeComponent() } ///  /// This method is used to set the sub-window title to the current time. ///  public void SetWindowTime() { this.Title = DateTime.Now.ToString() } ///  /// This method is used to get the main window and then call the SetMainWinowTime() method. ///  ///  ///  private void Button_Click(object sender, RoutedEventArgs e) { ((MainWindow)Application.Current.MainWindow).SetMainWinowTime() } }

効果チャート:

7、シングルインスタンスアプリケーション。

実装手順:

7.1)App.xamlファイルを削除します。

7.2)、Microsoft.VisualBasicアセンブリを参照します。

7.3)SignelInstanceApplicationクラスを作成し、それをApplicationクラスから継承し、クラスのOnStartupメソッド(ウィンドウのインスタンス化に使用)をオーバーライドし、Activate()メソッド(現在のウィンドウをアクティブ化する)を作成します。

7.4)3つの重要なメンバーを含むWindowsFormsApplicationBaseクラスを継承するSignelApplicationManagerクラスを作成します。

7.5)Startupクラスを記述し、その中にMainメソッドを記述して、SignelApplicationManagerクラスをインスタンス化します。

SignelInstanceApplicationクラス:

public class SignelInstanceApplication : System.Windows.Application { ///  /// Override the OnStartup() method to instantiate the main window in the method. ///  ///  protected override void OnStartup(System.Windows.StartupEventArgs e) { base.OnStartup(e) //Instantiate the window object (here the main window) in the OnStartup() method. MainWindow mw = new MainWindow() mw.Show() } 
/// /// This method is used to activate the current window (let the window display the front end). /// public void Activate() { System.Windows.MessageBox.Show('This program is already running') //Do other processing. this.MainWindow.Show() this.MainWindow.Activate() } }

SignelApplicationManagerクラス:

class SignelApplicationManager : WindowsFormsApplicationBase { //Declare the application object.  SignelInstanceApplication app ///  /// Set IsSingleInstance to singleton mode in the constructor. ///  public SignelApplicationManager() { //IsSingleInstance: Determines if this application is a singleton application. this.IsSingleInstance = true } ///  /// Instantiate the SignelInstanceApplication object in the OnStartup method. ///  ///  ///  protected override bool OnStartup(StartupEventArgs eventArgs) { base.OnStartup(eventArgs) //Instantiate the SignelInstanceApplication object. app = new SignelInstanceApplication() app.Run() //Returns false. return false } ///  /// The activation function is called in OnStartupNextInstance. ///  ///  protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) { base.OnStartupNextInstance(eventArgs) app.Activate() } }

スタートアップクラス:

public class Startup { [STAThread()] public static void Main(string[] args) { //Instantiate the SignelApplicationManager object. SignelApplicationManager sm = new SignelApplicationManager() //Call Run().  sm.Run(args) } }