質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

377閲覧

WPFを用いて、MainWindowページに画面遷移したい

ume

総合スコア3

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

1グッド

0クリップ

投稿2024/03/08 16:43

実現したいこと

Page3.xamlからMainWindow.xamlに、エラーなしで画面遷移したい

発生している問題・分からないこと

C#のWPFを用い、Page3.xamlというページから画面遷移をしてMainWindow.xamlというページに移動するアプリを作成したいと考えています。Visual Studio 2022上でビルドはうまくいったのですが、ボタンを押し、それに含まれるBackButton_Clickという関数をもとに画面遷移しようとすると例外が発生し、それ以上動作しなくなってしまいました。

エラーメッセージ

error

1System.InvalidOperationException: 'Window はツリーのルートです。Visual の子として Window を追加することはできません。'

該当のソースコード

C#(Page3.xaml.cs)

1using System.Text; 2using System.Windows; 3using System.Windows.Controls; 4using System.Windows.Data; 5using System.Windows.Documents; 6using System.Windows.Input; 7using System.Windows.Media; 8using System.Windows.Media.Imaging; 9using System.Windows.Navigation; 10using System.Windows.Shapes; 11 12namespace WpfApp1 13{ 14 /// <summary> 15 /// Page3.xaml の相互作用ロジック 16 /// </summary> 17 public partial class Page3 : Page 18 { 19 public Page3() 20 { 21 InitializeComponent(); 22 23 } 24 25 private void BackButton_Click(object sender, RoutedEventArgs e) 26 { 27 28 29 MainWindow mainWindow = new MainWindow(); 30 this.Content = mainWindow; 31 32 } 33 } 34} 35

C#(Page3.xaml)

1<Page x:Class="WpfApp1.Page3" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 d:DesignHeight="450" d:DesignWidth="800" 9 Background="AntiqueWhite" 10 Title="Page3"> 11 12 <Grid> 13 <Grid.RowDefinitions> 14 <RowDefinition Height="96*"/> 15 <RowDefinition Height="30*"/> 16 <RowDefinition Height="30*"/> 17 <RowDefinition Height="30*"/> 18 <RowDefinition Height="65*"/> 19 </Grid.RowDefinitions> 20 21 22 <TextBlock TextWrapping="Wrap" Text="タイトル" FontWeight="Bold" FontSize="50" Height="59" Width="219"/> 23 <TextBlock HorizontalAlignment="Left" Margin="100,0,0,0" TextWrapping="Wrap" Text="テキスト1" FontSize="28" VerticalAlignment="Center" Grid.Row="1" Height="36" Width="290" /> 24 <TextBlock HorizontalAlignment="Left" Margin="100,0,0,0" TextWrapping="Wrap" Text="テキスト2" FontSize="28" VerticalAlignment="Center" Width="571" Grid.Row="2" Height="38"/> 25 <TextBlock HorizontalAlignment="Left" Margin="100,5,0,0" TextWrapping="Wrap" Text="テキスト3" FontSize="28" VerticalAlignment="Center" Width="571" Grid.Row="3" Height="37"/> 26 27 <Button Content="もどる" FontSize="20" Width="120" Height="45" HorizontalAlignment="Center" Margin="0,10,0,0" Grid.Row="4" VerticalAlignment="Center" Cursor="Hand" Click="BackButton_Click" /> 28 <Image Source="Images/sample1.png" HorizontalAlignment="Left" Height="91" Margin="689,157,0,0" Grid.RowSpan="3" VerticalAlignment="Top" Width="79"/> 29 <Image Source="Images/sample2.png" HorizontalAlignment="Left" Height="100" Margin="676,33,0,0" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0.385,0.258" Grid.RowSpan="3" Grid.Row="2"/> 30 31 </Grid> 32</Page> 33

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

エラーメッセージを検索しましたが、自分の思うような内容を見つけられませんでした。

補足

Visual Studio 2022を使用。

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

画面遷移しようとすると例外が発生し、それ以上動作しなくなってしまいました。
MainWindow mainWindow = new MainWindow();
this.Content = mainWindow;

PageContentに、Windowを入れることはできません。

そうではなくてWindowContentを、Page1Page3に入れ替えます。
MainWindowにすでに内容があるなら、まるごとPageに移してください。

共通部分(メニュー等)がある場合は、Frameを使う方法もあります。
Frame クラス (System.Windows.Controls) | Microsoft Learn

ただFrameはイヤな点が多すぎて、個人的には嫌いです^^;
WPFでシンプルな独自ナビゲーション処理のサンプルを書いてみた - SourceChord

xml:MainWindow.xaml

1<Window 2 x:Class="Q3vv7cd16hrphze.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="clr-namespace:Q3vv7cd16hrphze" 6 Width="800" 7 Height="500" 8 MinWidth="450" 9 MinHeight="450"> 10 11 <!-- 起動時はPage1(Page3へボタンがある) --> 12 <local:Page1 /> 13 14 <!-- 共通部分(メニュー等)がある場合はFrameを使う --> 15 <!--<DockPanel> 16 <Menu DockPanel.Dock="Top"> 17 <MenuItem Header="ファイル(_F)" /> 18 </Menu> 19 <Frame NavigationUIVisibility="Hidden" Source="Page1.xaml" /> 20 </DockPanel>--> 21</Window>

xml:Page1.xaml

1<Page 2 x:Class="Q3vv7cd16hrphze.Page1" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 d:DesignHeight="450" 8 d:DesignWidth="800" 9 Background="Pink" 10 mc:Ignorable="d"> 11 <Grid> 12 <Grid.RowDefinitions> 13 <RowDefinition Height="170" /> 14 <RowDefinition Height="*" /> 15 <RowDefinition Height="Auto" /> 16 </Grid.RowDefinitions> 17 <TextBlock 18 HorizontalAlignment="Center" 19 VerticalAlignment="Center" 20 FontSize="50" 21 FontWeight="Bold" 22 Text="Page1" /> 23 <StackPanel 24 Grid.Row="1" 25 Margin="94,0,0,0" 26 TextBlock.FontSize="28"> 27 <TextBlock Margin="6" Text="テキスト1" /> 28 <TextBlock Margin="6" Text="テキスト2" /> 29 <TextBlock Margin="6" Text="テキスト3" /> 30 </StackPanel> 31 <Button 32 Grid.Row="2" 33 MinWidth="120" 34 MinHeight="45" 35 Margin="0,0,0,31" 36 HorizontalAlignment="Center" 37 VerticalAlignment="Bottom" 38 Click="Button_Click" 39 Content="go Page3" 40 Cursor="Hand" 41 FontSize="20" /> 42 <StackPanel 43 Grid.RowSpan="3" 44 Margin="0,150,10,0" 45 HorizontalAlignment="Right" 46 VerticalAlignment="Top"> 47 <Image 48 Width="79" 49 Height="91" 50 Source="https://www.gravatar.com/avatar/948a7baeeb9edd89de3c879ebd14f3ac?d=identicon" /> 51 <Image 52 Width="100" 53 Height="100" 54 Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg" /> 55 </StackPanel> 56 </Grid> 57</Page>

cs:Page1.xaml.cs

1using System; 2using System.Windows; 3using System.Windows.Controls; 4 5namespace Q3vv7cd16hrphze; 6 7 8public partial class Page1 : Page 9{ 10 public Page1() => InitializeComponent(); 11 12 private void Button_Click(object sender, RoutedEventArgs e) 13 { 14 // MainWindowのContentをPage3に入れ替える 15 Application.Current.MainWindow.Content = new Page3(); 16 17 // Frameを使う場合の遷移 18 //NavigationService.Navigate(new Uri("Page3.xaml", UriKind.Relative)); 19 } 20}

xml:Page3.xaml

1<Page 2 x:Class="Q3vv7cd16hrphze.Page3" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 d:DesignHeight="450" 8 d:DesignWidth="800" 9 Background="AntiqueWhite" 10 mc:Ignorable="d"> 11 <Grid> 12 <Grid.RowDefinitions> 13 <RowDefinition Height="96*" /> 14 <RowDefinition Height="30*" /> 15 <RowDefinition Height="30*" /> 16 <RowDefinition Height="30*" /> 17 <RowDefinition Height="65*" /> 18 </Grid.RowDefinitions> 19 <TextBlock 20 Width="164" 21 Height="75" 22 FontSize="50" 23 FontWeight="Bold" 24 Text="Page3" /> 25 <TextBlock 26 Grid.Row="1" 27 Width="290" 28 Height="36" 29 Margin="100,0,0,0" 30 HorizontalAlignment="Left" 31 FontSize="28" 32 Text="テキスト1" /> 33 <TextBlock 34 Grid.Row="2" 35 Width="571" 36 Height="38" 37 Margin="100,0,0,0" 38 HorizontalAlignment="Left" 39 FontSize="28" 40 Text="テキスト2" /> 41 <TextBlock 42 Grid.Row="3" 43 Width="571" 44 Height="37" 45 Margin="100,5,0,0" 46 HorizontalAlignment="Left" 47 FontSize="28" 48 Text="テキスト3" /> 49 <Button 50 Grid.Row="4" 51 Width="120" 52 Height="45" 53 Margin="0,10,0,0" 54 HorizontalAlignment="Center" 55 VerticalAlignment="Center" 56 Click="BackButton_Click" 57 Content="もどる" 58 Cursor="Hand" 59 FontSize="20" /> 60 <Image 61 Grid.RowSpan="3" 62 Width="79" 63 Height="91" 64 Margin="689,157,0,0" 65 HorizontalAlignment="Left" 66 VerticalAlignment="Top" 67 Source="https://www.gravatar.com/avatar/948a7baeeb9edd89de3c879ebd14f3ac?d=identicon" /> 68 <Image 69 Grid.Row="2" 70 Grid.RowSpan="3" 71 Width="100" 72 Height="100" 73 Margin="676,33,0,0" 74 HorizontalAlignment="Left" 75 VerticalAlignment="Top" 76 Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg" /> 77 </Grid> 78</Page>

cs:Page3.xaml.cs

1using System; 2using System.Windows; 3using System.Windows.Controls; 4 5namespace Q3vv7cd16hrphze; 6 7 8public partial class Page3 : Page 9{ 10 public Page3() => InitializeComponent(); 11 12 private void BackButton_Click(object sender, RoutedEventArgs e) 13 { 14 // MainWindowのContentをPage1に入れ替える 15 Application.Current.MainWindow.Content = new Page1(); 16 17 // Frameを使う場合の遷移 18 //NavigationService.Navigate(new Uri("Page1.xaml", UriKind.Relative)); 19 } 20}

アプリ動画
どこが伸縮すべきかわからないですが、画像の見切れと文字の上下動がちょっと気になります(リサイズ不可なら構いません)


個人的には↓のような方法が好みです。

投稿2024/03/08 23:47

TN8001

総合スコア9363

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ume

2024/03/09 11:59 編集

回答ありがとうございます! MainWindowの内容を別ページに差し替え、最初に表示される画面も変更したところ、自分の思い描くような動作ができました! 前回に引き続き、お助けしてくださってありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問