HUSKING - kotteri

技術系Note

【C#】WPFアプリケーションにて起動および終了を制御する

App.xamlを編集

アプリ起動時に呼ばれる画面の設定を削除
・編集前

<Application x:Class="TimeCard.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TimeCard"
    StartupUri="MainWindow.xaml">

↓ StartupUri を削除

・編集後

<Application x:Class="TimeCard.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TimeCard">

App.xaml.csを編集

アプリ起動および終了時の処理を追記

/// <summary>
/// アプリケーションが終了する時のイベント
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
    // 初期表示画面を呼び出し
    MainWindow window = new MainWindow();
    window.Show();
}

/// <summary>
/// アプリケーションが終了する時のイベント
/// </summary>
/// <param name="e"></param>
protected override void OnExit(ExitEventArgs e)
{
    // 終了時なにかしたければ記入
}