アットウィキロゴ

開発部 > 技術 > Visual Studio 2008 > WPF > 画面遷移と値の受け渡し

WPFでの画面遷移方法とページ間で値を受け渡す方法について紹介します。

WPFではNavigationServiceクラスを利用して画面遷移を実現します。
画面1⇒画面2という感じに画面遷移するサンプルを作ってみたいと思います。

画面1では画面2に渡す文字列と文字色を入力し、ハイパーリンクをクリックすることで画面2に遷移します。
XAMLはこんな感じです。
  1. <Page x:Class="TestApplication.NavigationTest1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="NavigationTest1" Loaded="Page_Loaded">
  5. <StackPanel>
  6. <TextBox Name="txtTest" Width="150" Height="24" HorizontalAlignment="Left" Margin="5"></TextBox>
  7. <ComboBox Height="24" Name="cboColors" Width="120" HorizontalAlignment="Left" Margin="5" />
  8. <TextBlock Margin="5">
  9. 次のページに移動するには<Hyperlink Name="hlTest" Click="hlTest_Click">ここ</Hyperlink>をクリックしてください。
  10. </TextBlock>
  11. </StackPanel>
  12. </Page>
  13.  

画面2では画面1より渡された文字列、文字色をTextBlockに設定します。
XAMLはこんな感じ。
  1. <Page x:Class="TestApplication.NavigationTest2"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="NavigationTest2" Loaded="Page_Loaded">
  5. <Page.Background>
  6. <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
  7. <GradientStop Color="Black" Offset="0"/>
  8. <GradientStop Color="DarkGray" Offset="1"/>
  9. </LinearGradientBrush>
  10. </Page.Background>
  11. <StackPanel>
  12. <TextBlock Name="tbApplication" Margin="10" FontSize="30" />
  13. </StackPanel>
  14. </Page>
  15.  

ではC#コードのほうを見ていきましょう。
画面1のC#コードです。
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4. using System.Windows.Navigation;
  5. using System.Reflection;
  6.  
  7. namespace TestApplication
  8. {
  9. /// <summary>
  10. /// NavigationTest1.xaml の相互作用ロジック
  11. /// </summary>
  12. public partial class NavigationTest1 : Page
  13. {
  14. public NavigationTest1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void Page_Loaded(object sender, RoutedEventArgs e)
  20. {
  21. // コンボボックスにColorsクラスの一覧を設定
  22. foreach (PropertyInfo info in typeof(Colors).GetProperties())
  23. {
  24. cboColors.Items.Add(info.Name);
  25. }
  26. cboColors.SelectedIndex = 0;
  27.  
  28. }
  29.  
  30. private void hlTest_Click(object sender, RoutedEventArgs e)
  31. {
  32. // Applicationオブジェクトにテキストボックスの値を格納
  33. Application.Current.Properties["input"] = txtTest.Text;
  34.  
  35. // 画面2を生成
  36. NavigationTest2 next = new NavigationTest2();
  37.  
  38. // 画面2のプロパティに値を設定
  39. next.TextBlockForeground =
  40. new SolidColorBrush((Color)ColorConverter.ConvertFromString(cboColors.SelectedItem.ToString()));
  41.  
  42. // 画面2に遷移
  43. NavigationService.Navigate(next);
  44. }
  45. }
  46. }
  47.  

画面2のC#コードです。
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4.  
  5. namespace TestApplication
  6. {
  7. /// <summary>
  8. /// NavigationTest2.xaml の相互作用ロジック
  9. /// </summary>
  10. public partial class NavigationTest2 : Page
  11. {
  12. private SolidColorBrush _textBlockForeground = new SolidColorBrush();
  13.  
  14. public SolidColorBrush TextBlockForeground
  15. {
  16. get; set;
  17. }
  18.  
  19. public NavigationTest2()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void Page_Loaded(object sender, RoutedEventArgs e)
  25. {
  26. tbApplication.Text = Application.Current.Properties["input"].ToString();
  27. tbApplication.Foreground = this.TextBlockForeground;
  28. }
  29. }
  30. }
  31.  

画面1のPage_LoadedでコンボボックスにColorsクラスの一覧を設定しています。
この処理に関しての詳しい説明はクラスのプロパティの一覧をComboBoxに設定するを参照してください。

ページ間での値の受け渡し方法には以下の2通りがあります。
  • ApplicationオブジェクトのPropertiesを使用する。
  • 遷移先ページにプロパティを用意し、そこに値を設定する。

今回は画面2のTextBlockのテキストにApplicationオブジェクトから値を設定し、文字色は画面2のプロパティより設定しています。
両方とも紹介するためにこのようにしましたが、本来はちゃんと使い分けをしなくてはなりませんね。
ApplicationオブジェクトのPropertiesの値は明示的にクリアしない限り、アプリが実行されている間ずっと存在し続けるので気をつけなければいけません。
たくさんのページで共通で使用する値等を渡すときに採用するのが良さそうですね。
むしろ、そういった場合しか使わないほうがよいでしょう。

次に画面遷移方法ですが、前述したとおりNavigationServiceクラスを利用します。
画面2のインスタンスを生成してNavigationServiceクラスのNavigateメソッドに渡すことによって画面遷移を実現します。

では実行してみます。


で、リンクをクリックしてみると・・・


うまく行きました!
ちゃんとブラウザの戻るボタンも使えます~。
画面の色はなんとなく変えてみました♪






最終更新:2009年05月18日 12:06
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。
添付ファイル