プロジェクトの作成で、WPFアプリを選択し、ビルドすると自動生成されたMainWindowが表示されます。
このMainWindowの表示をさせない(=プログラムで制御)する方法を以下に記します。
ウインドウ表示前に何かの処理をして表示・非表示を制御するようなときに使えると思います。
修正箇所を以下に記します。
XAML,C#ソースを修正します。
StartupUriを削除します。
<Application x:Class="NotDispMainWindowCS.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:プロジェクト名" StartupUri="MainWindow.xaml"> <Application.Resources> </Application.Resources> </Application>
<Application x:Class="NotDispMainWindowCS.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:プロジェクト名"> <Application.Resources> </Application.Resources> </Application>
/// <summary> /// App.xaml の相互作用ロジック /// </summary> public partial class App : Application { }
/// <summary> /// App.xaml の相互作用ロジック /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var w = new MainWindow(); w.Show(); } protected override void OnExit(ExitEventArgs e) { base.OnExit(e); } }
XAML,VBソースを修正します。
StartupUriを削除します。
<Application x:Class="Application" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:プロジェクト名" StartupUri="MainWindow.xaml"> <Application.Resources> </Application.Resources> </Application>
<Application x:Class="Application" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:プロジェクト名"> <Application.Resources> </Application.Resources> </Application>
Class Application ' Startup、Exit、DispatcherUnhandledException などのアプリケーション レベルのイベントは、 ' このファイルで処理できます。 End Class
Class Application Protected Overrides Sub OnStartup(e As StartupEventArgs) MyBase.OnStartup(e) Dim w = New MainWindow() w.Show() End Sub Protected Overrides Sub OnExit(e As ExitEventArgs) MyBase.OnExit(e) End Sub End Class
以上、WPFアプリでMainWindowの表示/非表示をプログラムで制御する方法でした。