Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

アプリケーション設定をWPFで利用する

使い方は、Windowsフォームアプリケーションやコンソールアプリケーションと同じです。

アプリケーション設定を作成する


App.xaml

マークアップ
<Application x:Class="HelloWorld.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>
ビハインドコード
using System.Windows;

namespace HelloWorld {
    /// <summary>
    /// App.xaml の相互作用ロジック
    /// </summary>
    public partial class App : Application {
    }
}

MainWindow.xaml

マークアップ
<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        
    </Grid>
</Window>
ビハインドコード
using System.Windows;
using HelloWorld.Properties;

namespace HelloWorld {
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();

            this.Loaded += (s, e) => {
                Settings.Default.RunCount += 1;
                this.Title = string.Format("実行回数は{0}回です", Settings.Default.RunCount);
            };

            this.Closed += (s, e) => { Settings.Default.Save(); };
        }
    }
}

アプリ実行

1回目


2回目


参考

FrameworkElement.Loaded イベント (System.Windows)
要素の配置、描画、および操作の準備が完了したときに発生します。 ...

Window.Closed イベント (System.Windows)
ウィンドウが閉じるときに発生します。 ...