前提
App.xaml のビルドアクションを標準の ApplicationDefinition から Page へ変更し、Main メソッドを自分で定義しており、NavigationWindow を初期表示しています。
NavigationWindow は
- SizeToContent プロパティを WidthAndHeight
- WindowStartupLocation プロパティを CenterScreen
- Source プロパティはは初期ページ(XAML)の URI
- サイズは未指定
に指定しています。
実現したいこと
自分でインスタンス生成したウインドウを画面中央に表示したい。
発生している問題
App.StartupUri に NavigationWindow の XAML ファイルへのパスを指定した場合は、Source で指定したページを読み込んだ NavigationWindow が画面中央に表示されますが、
直接 NavigationWindow のインスタンスを生成し、Show メソッドで表示(または App.Run の引数にインスタンスを設定)した場合は、Sourceで指定したページが読み込まれていない状態で一瞬画面中央に表示された後初期ページへ遷移するため、ウインドウサイズ分右下にずれて表示されてしまいます。
もう少し詳しく書くと、
App.StartupUri を指定した場合は、
- NavigationWindow をロード
- Source で指定された Page をロード(この時、NavigationWindow がリサイズ)
- NavigationWindow を画面中央に表示
される。
自分で NavigationWindow のインスタンスを生成した場合、
- NavigationWindow をロード
- NavigationWindow を画面中央に表示
- Source で指定された Page をロード(この時、NavigationWindow がリサイズ)
の順番で処理されている様に見えます。
自分で NavigationWindow のインスタンスを生成した場合でも、App.StartupUri を指定した場合と同じように動いて欲しいです。
該当のソースコード
- App.xaml
XAML
1<Application x:Class="Test.App" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 4</Application>
- App.xaml.cs
C#
1using System; 2using System.Windows; 3using Test.Views; 4 5namespace Test { 6 public partial class App: Application { 7 [STAThread()] 8 public static void Main( string[] args ) { 9 var app = new App(); 10 app.InitializeComponent(); 11#if false 12 //こちらを有効にすると、ページ遷移後のウインドウサイズで画面中央に表示される 13 app.StartupUri = new Uri( "Views/MainWindow.xaml", UriKind.Relative ); 14#else 15 //こちらはページ遷移後のウインドウサイズ分、画面中央より右下にずれて表示される 16 var window = new MainWindow(); 17 window.InitializeComponent(); //指摘があったので修正 18 window.Show(); 19#endif 20 app.Run(); 21 } 22 } 23} 24
- MainWindow.xaml(コードビハインドは何も変更していないため省略します。)
XAML
1<NavigationWindow 2 x:Class="Test.Views.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 6 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 7 xmlns:v="clr-namespace:Test.Views" 8 Title="MainWindow" 9 SizeToContent="WidthAndHeight" 10 WindowStartupLocation="CenterScreen" 11 Source="DummyPage.xaml"> 12</NavigationWindow>
- MainWindow.xaml.cs
コードビハインドは何も変更していないため省略します。
- DummyPage.xaml(テスト用のダミーのため、Buttonコントロールに意味はありません。)
XAML
1<Page 2 x:Class="Test.Views.DummyPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 6 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 7 xmlns:v="clr-namespace:Test.Views" 8 Title="MainPage" 9 Height="350" 10 Width="525"> 11 <Grid> 12 <Button Content="Button" Height="140" Width="325"/> 13 </Grid> 14</Page>
- DummyPage.xaml.cs
コードビハインドは何も変更していないため省略します。
試したこと
- App.Run の引数に NavigationWindow のインスタンスを指定する。
- Window.Show 前に Window.Hide 呼び出し。
- Window.Show 前に WindowStartupLocation に CenterScreen を指定する。
- オーバーライドした App.OnStartup 内で、window.show する。
補足情報(FW/ツールのバージョンなど)
Visual Studio 2017
.NET Framework 4.7.1

回答3件
あなたの回答
tips
プレビュー