Wpf Appの初回起動画面の変更の仕方
初回起動画面の変更方法
Visual StudioのWpfアプリケーションで、初回起動画面の変更の仕方を記載します。
Wpfのプロジェクトを新規作成すると、下記のような構成でプロジェクトが立ち上がります。
・App.xaml
・MainWindow.xaml

デバッグすると、下記のようなに自動でMainWindowが表示されます。
今回は、初回起動時に表示されるウィンドウの変更方法を説明します。

下記の画像のように、プロジェクトにStartWindow.xamlを追加します。
今回は、MainWidow.xamlからStartWindow.xamlに変更します。

App.xamlを開くと下記のようなソースコードが記載されています。
<Application x:Class="WpfPractice.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfPractice"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
StartupUri=”MainWindow.xaml” から StartupUri=”StartWindow.xaml”に変更します。
”****.xaml”と初回起動で表示させたいxaml名に変更するだけでOKです。

実践的な起動処理の変更方法
上記の方法は、起動画面の変更方法として最も簡便なやり方です。実際の業務では初回起動画面のほかにも様々な起動処理を加えたいかと思います。そんな時は下記のように変更してください。
下記の例では、アプリケーションの2重起動防止処理を加えつつ、初回起動画面を変更しています。
App.xamlクラス
StartupUriからStarupに変更します。
<Application x:Class="WpfApp2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.csクラス
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static System.Threading.Mutex mutex = new System.Threading.Mutex(false, "WpfPractice");
public void StartUpApp(object sender, StartupEventArgs e)
{
//多重起動チェック
// ミューテックスの所有権を要求
if (!mutex.WaitOne(0, false))
{
// 既に起動しているため終了させる
MessageBox.Show("ApplicationName は既に起動しています。", "二重起動防止", MessageBoxButton.OK, MessageBoxImage.Exclamation);
mutex.Close();
mutex = null;
this.Shutdown();
}
//Windowの表示
StartWindow startWindow = new StartWindow();
startWindow.Show();
}
}
