実現したいこと
Visual Studio 2022において、wpf(C#)によるアプリ作成の勉強をはじめました。
- ボタン”Page1”をクリックしたら、Page1.xamlで定義した画面に
- ボタン"Page2"をクリックしたら、Page2.xamlで定義した画面に
リスト表示が切り替わる機能を実現したいです。
ネットの情報を参考にトライしているのですが、うまく動作しないようです。
非常に初歩的な質問で恐縮ですが、問題点、修正方法をお教えいただければ助かります。
前提
プログラム全般は少しかじった程度の初心者です。
プログラムのコンパイル、リンクは可能で、プログラムは動作しています。
発生している問題・エラーメッセージ
ボタン”Page1”をクリックしたら、Button_Click_1が実行されることを確認する為のメッセージは正常に表示されますが、"WpfApp13.Page1"と文字が表示されるだけで、xamlで定義した表示になりません。 ボタン"Page2"をクリックしたら、"WpfApp13.Page2"と文字が表示されるだけで、xamlで定義した表示になりません。
該当のソースコード
MainWindow.xaml
1<Window x:Class="WpfApp13.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp13" 7 mc:Ignorable="d" 8 Title="MainWindow"> 9 10 <StackPanel> 11 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 12 <Button Content="Page1" Click="Button_Click_1"/> 13 <Button Content="Page2" Click="Button_Click_2"/> 14 </StackPanel> 15 <Frame x:Name="contentFrame"/> 16 </StackPanel> 17 18</Window>
MainWindow.xaml.cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15 16namespace WpfApp13 17{ 18 /// <summary> 19 /// Interaction logic for MainWindow.xaml 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 public MainWindow() 24 { 25 InitializeComponent(); 26 } 27 28 //private void Button1_Click(object sender, RoutedEventArgs e) 29 //{ 30 // MessageBox.Show("クリックされました。"); 31 // contentFrame.Navigate(typeof(Page1)); 32 //} 33 //Page1へナビゲーションする 34 private void Button_Click_1(object sender, RoutedEventArgs e) 35 { 36 MessageBox.Show("クリックされました。"); 37 contentFrame.Navigate(typeof(WpfApp13.Page1)); 38 } 39 40 //Page2へナビゲーションする 41 private void Button_Click_2(object sender, RoutedEventArgs e) 42 { 43 contentFrame.Navigate(typeof(WpfApp13.Page2)); 44 45 } 46 47 } 48} 49
Page1.xaml
1<Page x:Class="WpfApp13.Page1" 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:WpfApp13" 7 mc:Ignorable="d" 8 d:DesignHeight="450" d:DesignWidth="800" 9 10 Title="Page1"> 11 12 <StackPanel Height="1000" HorizontalAlignment="Center" Background="LawnGreen"> 13 <TextBlock Text="Page 1" FontSize="80"/> 14 </StackPanel> 15</Page>
Page1.xaml.cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15 16namespace WpfApp13 17{ 18 /// <summary> 19 /// Page1.xaml の相互作用ロジック 20 /// </summary> 21 public partial class Page1 : Page 22 { 23 public Page1() 24 { 25 InitializeComponent(); 26 } 27 } 28}
Page2.xaml
1<Page x:Class="WpfApp13.Page2" 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:WpfApp13" 7 mc:Ignorable="d" 8 d:DesignHeight="450" d:DesignWidth="800" 9 Title="Page2"> 10 11 <StackPanel Height="450" HorizontalAlignment="Center" Background="Orange"> 12 <TextBlock Text="Page 2" FontSize="40"/> 13 </StackPanel> 14</Page>
Page2.xaml.cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15 16namespace WpfApp13 17{ 18 /// <summary> 19 /// Page2.xaml の相互作用ロジック 20 /// </summary> 21 public partial class Page2 : Page 22 { 23 public Page2() 24 { 25 InitializeComponent(); 26 } 27 } 28}
補足情報(FW/ツールのバージョンなど)
Microsoft Visual Studio Community 2022 (64 ビット)
Version 17.0.4

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/04/16 05:10
2023/04/16 05:25
2023/04/17 00:18