teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

InitializeComponent() の追加箇所間違ってたので修正。

2019/03/19 06:37

投稿

draq
draq

スコア2577

title CHANGED
File without changes
body CHANGED
@@ -52,6 +52,7 @@
52
52
  [STAThread()]
53
53
  public static void Main( string[] args ) {
54
54
  var app = new App();
55
+ app.InitializeComponent();
55
56
  #if false
56
57
  //こちらを有効にすると、ページ遷移後のウインドウサイズで画面中央に表示される
57
58
  app.StartupUri = new Uri( "Views/MainWindow.xaml", UriKind.Relative );

1

window.InitializeComponent() の呼び出しを追加(結果は変わらないが、呼び出しは必要なのでコードを修正しました。)と実現したいことをもっと詳細に記載しました。

2019/03/19 06:37

投稿

draq
draq

スコア2577

title CHANGED
File without changes
body CHANGED
@@ -11,13 +11,27 @@
11
11
  に指定しています。
12
12
 
13
13
  ### 実現したいこと
14
- ウインドウを画面中央に表示したい。
14
+ 自分でインスタンス生成したウインドウを画面中央に表示したい。
15
15
 
16
16
  ### 発生している問題
17
17
  App.StartupUri に NavigationWindow の XAML ファイルへのパスを指定した場合は、Source で指定したページを読み込んだ NavigationWindow が画面中央に表示されますが、
18
18
  直接 NavigationWindow のインスタンスを生成し、Show メソッドで表示(または App.Run の引数にインスタンスを設定)した場合は、Sourceで指定したページが読み込まれていない状態で一瞬画面中央に表示された後初期ページへ遷移するため、ウインドウサイズ分右下にずれて表示されてしまいます。
19
19
 
20
+ もう少し詳しく書くと、
21
+ App.StartupUri を指定した場合は、
22
+ 1. NavigationWindow をロード
23
+ 1. Source で指定された Page をロード(この時、NavigationWindow がリサイズ)
24
+ 1. NavigationWindow を画面中央に表示
20
25
 
26
+ される。
27
+ 自分で NavigationWindow のインスタンスを生成した場合、
28
+ 1. NavigationWindow をロード
29
+ 1. NavigationWindow を画面中央に表示
30
+ 1. Source で指定された Page をロード(この時、NavigationWindow がリサイズ)
31
+
32
+ の順番で処理されている様に見えます。
33
+ 自分で NavigationWindow のインスタンスを生成した場合でも、App.StartupUri を指定した場合と同じように動いて欲しいです。
34
+
21
35
  ### 該当のソースコード
22
36
 
23
37
  - App.xaml
@@ -44,6 +58,7 @@
44
58
  #else
45
59
  //こちらはページ遷移後のウインドウサイズ分、画面中央より右下にずれて表示される
46
60
  var window = new MainWindow();
61
+ window.InitializeComponent(); //指摘があったので修正
47
62
  window.Show();
48
63
  #endif
49
64
  app.Run();
@@ -52,7 +67,8 @@
52
67
  }
53
68
 
54
69
  ```
70
+
55
- - MainWindow.xaml
71
+ - MainWindow.xaml(コードビハインドは何も変更していないため省略します。)
56
72
  ```XAML
57
73
  <NavigationWindow
58
74
  x:Class="Test.Views.MainWindow"
@@ -70,6 +86,26 @@
70
86
  - MainWindow.xaml.cs
71
87
  コードビハインドは何も変更していないため省略します。
72
88
 
89
+ - DummyPage.xaml(テスト用のダミーのため、Buttonコントロールに意味はありません。)
90
+ ```XAML
91
+ <Page
92
+ x:Class="Test.Views.DummyPage"
93
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
94
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
95
+ xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
96
+ xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
97
+ xmlns:v="clr-namespace:Test.Views"
98
+ Title="MainPage"
99
+ Height="350"
100
+ Width="525">
101
+ <Grid>
102
+ <Button Content="Button" Height="140" Width="325"/>
103
+ </Grid>
104
+ </Page>
105
+ ```
106
+ - DummyPage.xaml.cs
107
+ コードビハインドは何も変更していないため省略します。
108
+
73
109
  ### 試したこと
74
110
  - App.Run の引数に NavigationWindow のインスタンスを指定する。
75
111
  - Window.Show 前に Window.Hide 呼び出し。