実現したいこと
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を使用。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/03/09 11:59 編集