画面の切り替えを行います。サンプルではレイアウトも記述しているためソースが長めになっていますが、必要なものはページを切り替える「NavigationService.Navigate」と、切り替え先で呼び出されるOnNavigatedToメソッドのみです。また、ページを切り替える直前で呼ばれるメソッドは OnNavigatedFromになっています。
<phone:PhoneApplicationPage x:Class="BaseApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Canvas x:Name="LayoutRoot" Background="Black"> <Button Canvas.Left="116" Canvas.Top="121" Content="サブページへ" Height="130" Name="button1" Width="300" Click="ChangePage_Click" /> </Canvas> </phone:PhoneApplicationPage>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace BaseApp { // エントリーポイント public partial class MainPage : PhoneApplicationPage { // コンストラクタ public MainPage() { // コンポーネントの初期化。 InitializeComponent(); } // ページ遷移開始時に呼ばれるメソッド。 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { System.Diagnostics.Debug.WriteLine("call OnNavigatedFrom"); } // 「サブページへ」がクリックされた。 private void ChangePage_Click(object sender, RoutedEventArgs e) { this.NavigationService.Navigate(new Uri("/SubPage.xaml", UriKind.Relative)); } } }
<phone:PhoneApplicationPage x:Class="BaseApp.SubPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Canvas x:Name="LayoutRoot" Background="Black"></Canvas> </phone:PhoneApplicationPage>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace BaseApp { // エントリーポイント public partial class SubPage : PhoneApplicationPage { // コンストラクタ public SubPage() { } // NavigationService.Navigate後に呼ばれるメソッド。 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { System.Diagnostics.Debug.WriteLine("call OnNavigatedTo"); } }