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

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

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

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

Q&A

解決済

1回答

1058閲覧

【WPF, C#】Frame内でPageを遷移させる方法を知りたい

saku26

総合スコア7

C#

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

1グッド

0クリップ

投稿2023/04/15 14:18

編集2023/04/15 14:49

実現したいこと

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

TN8001👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

ネットの情報を参考にトライしているのですが、うまく動作しないようです。

参考サイトは質問に明記してください。

おそらくこちらを参考にしたんだと思います。
Frame内でPageを遷移させる(ナビゲーション)【C#/XAML】 | BioTech ラボ・ノート

ですがよく見てください。

開発環境
.NET 6.0
WinUI 3.0 (Windows App SDK 1.0)

となっています。

wpf frame page」とかで検索すると上位に表示されますが、WPFの記事ではありません!!

これはWinUI(UWP)用で、WPFのFrameTypeをとるオーバーロードはありません。

Uriを指定するか、
Frame.Navigate メソッド (System.Windows.Controls) | Microsoft Learn

ObjectPage自体)を渡します。
Frame.Navigate メソッド (System.Windows.Controls) | Microsoft Learn

cs

1using System; 2using System.Diagnostics; 3using System.Windows; 4 5namespace Qeriyjcmpjew8i3 6{ 7 8 public partial class MainWindow : Window 9 { 10 public MainWindow() => InitializeComponent(); 11 12 //Page1へナビゲーションする 13 private void Button_Click_1(object sender, RoutedEventArgs e) 14 { 15 Debug.WriteLine("Navigate Page1"); 16 contentFrame.Navigate(new Uri("Page1.xaml", UriKind.Relative)); 17 } 18 19 //Page2へナビゲーションする 20 private void Button_Click_2(object sender, RoutedEventArgs e) 21 { 22 Debug.WriteLine("Navigate Page2"); 23 contentFrame.Navigate(new Page2()); 24 } 25 } 26}

投稿2023/04/15 21:40

TN8001

総合スコア9321

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

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

saku26

2023/04/16 05:10

TN8001様 おっしゃる通り、そちらのサイト等を参考にしておりました。 開発環境の違いが原因であっても、コンパイルエラーにならない場合もあるのですね。 ご教示いただいたコードで、意図通り動作することが確認できました。ありがとうございます。 お教えいただいたサイトも参考に、理解を深めていきたいと思います。
TN8001

2023/04/16 05:25

> 開発環境の違いが原因であっても、コンパイルエラーにならない場合もあるのですね。 Navigate(Object)があるのでTypeでもなんでも通ってしまうのですが、期待しているものでなかった場合ToString()しているものと思われます。
saku26

2023/04/17 00:18

なるほどです。 エラーにならない不具合は、気づきにくいので、より注意が必要ですね。 大変助かりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問