WPFのGUIを立ち上げたり閉じる際に何かしらの変数を参照してGUIに反映したいとする。この時方法はいくつかあるが、その内の一つがアプリケーション設定”Properties.Settings.Default”の利用である。
アプリケーション設定”Properties.Settings.Default”の利用方法について述べる。
初期値の設定
適当なWPFプロジェクトを新規で作成
プロジェクトを右クリックしプロパティを開く
Setting画面を開く
適当な値を設定する。
”Properties.Settings.Default”設定値の利用
Setting画面で設定したNameをProperties.Settings.Default.AとProperties.Settings.Default.Bのように利用可能。
public MainWindow() { InitializeComponent(); textBlockA.Text = Properties.Settings.Default.A; textBlockB.Text= Properties.Settings.Default.B; }
出力結果
”Properties.Settings.Default”設定値の上書き方法
Properties.Settings.Default.Save();を用いる。
public MainWindow() { InitializeComponent(); textBlockA.Text = Properties.Settings.Default.A; textBlockB.Text= Properties.Settings.Default.B; Properties.Settings.Default.A = "New A"; Properties.Settings.Default.B = "New B"; Properties.Settings.Default.Save(); textBlockA.Text = Properties.Settings.Default.A; textBlockB.Text = Properties.Settings.Default.B; }