▲ この画面は シミュレータの画面ですデフォルトでは、画面のコントロールは private になっているようで、x:FieldModifier="public" を設定する事によって、他のページのコードから参照可能になります( デザイナ時点で可 )![]()
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Loaded="Page_Loaded">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button
x:FieldModifier="public"
x:Name="MoveButton"
Content="このボタンは x:FieldModifier="public" です"
HorizontalAlignment="Left"
Height="61"
Margin="66,64,0,0"
VerticalAlignment="Top"
Width="379"
Click="MoveButton_Click" />
</Grid>
</Page>
App.xaml.cs に、ページ移動や参照用のメソッドや変数を作成しています。 ❶ public static DictionaryAppPage ※ ページの実体保存用 ❷ public static Frame rootFrame ※ ページの格納用( 元々ローカル変数だったものを public static に変更 ) ❸ public static void PageOpen(Type name) ※ ページ移動用 ❹ public static T GetPage (Type name) ※ ページ参照用 App.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace App1
{
sealed partial class App : Application
{
public static Dictionary<Type , Page> AppPage = new Dictionary<Type , Page>();
public static Frame rootFrame = Window.Current.Content as Frame;
public static void PageOpen(Type name)
{
Page page = null;
if (!App.AppPage.TryGetValue(name, out page))
{
App.rootFrame.Navigate(name);
}
// 2回目以降の次ページへの移動
else
{
App.rootFrame.Content = App.AppPage[name];
}
}
public static T GetPage<T>(Type name)
{
return (T)(object)App.AppPage[name];
}
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
if (rootFrame == null)
{
rootFrame = new Frame();
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: 以前中断したアプリケーションから状態を読み込みます。
}
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
{
throw new Exception("Failed to create initial page");
}
}
// 現在のウィンドウがアクティブであることを確認します
Window.Current.Activate();
}
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: アプリケーションの状態を保存してバックグラウンドの動作があれば停止します
deferral.Complete();
}
}
}
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
App.AppPage.Add(typeof(MainPage), this);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 画面が表示される初回だけ実行されます
Debug.WriteLine("OnNavigatedTo");
}
private void MoveButton_Click(object sender, RoutedEventArgs e)
{
App.PageOpen(typeof(BlankPage1));
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
// 画面が表示される毎に実行されます
Debug.WriteLine("Page_Loaded");
}
}
}
画面移動のたびに、画面が表示された時の処理を行うには、loaded イベント を使用します。protected override void OnNavigatedTo は、Navigate メソッド(ここでは、App.rootFrame.Navigate)が実行される最初だけ処理されます。 BlankPage1.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace App1
{
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
App.AppPage.Add(typeof(BlankPage1), this);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 画面が表示される初回だけ実行されます
Debug.WriteLine("OnNavigatedTo");
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
App.PageOpen(typeof(MainPage));
MainPage mp = null;
// 変数を使った参照
mp = App.AppPage[typeof(MainPage)] as MainPage;
Debug.WriteLine(mp.MoveButton.Content.ToString());
// メソッドを使った参照
mp = App.GetPage<MainPage>(typeof(MainPage));
Debug.WriteLine(mp.MoveButton.Content.ToString());
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
// 画面が表示される毎に実行されます
Debug.WriteLine("Page_Loaded");
}
}
}
関連する記事 XAML の 各属性を別の行に配置する Visual Studio の設定
|
|
【Win8 ストアアプリの最新記事】
- C# : HttpClient で Post と Get する汎用 static クラス
- Win8.1 ストアアプリ(JS) : Visual Studio 2013 で Three.js(v65) の WebGLRenderer の動作を確認しました
- WinJS ストア : Three.js を組み込んで、『画像を飛ばす』テンプレート( Bird.js を利用 )
- WinJS ストア : 『背景画像をチェンジする2画面アプリ』のテンプレート
- VS2012ストア(C#) : WebView テンプレート
- VS2012(C#)ストア : ListView Twitter 検索テンプレート
- イラストを背景にして2ページの画面遷移を解りやすくした Windows Store テンプレート
- Twitter API の自分のアプリのトークンを使って投稿するだけの class VS2012_Twitter
- Win8 ストア(C#) / PDF viewer sample (Windows 8.1)
- ストアアプリの TextBox のスクロールバー
- Win8 ストアアプリで、『選択肢を応答するダイアログ』を簡単に使うための MessageBox クラス
- Win8 ストアから Post 投稿
- Win8ストア XAML の AppBarButtonStyle のContent に指定する 16進数 Unicode の取得
- Win8 ストア : UrlEncode と UrlDecode
- Win8 ストア : HttpClient + XDocument で RSS の取得
- Win8 ストア : リストボックス テンプレート
- Win8 ストア : ファイルアクセス テンプレート
- Win8 ストア : ストアブランク テンプレート
- AppBar テンプレート / Win8 ストアアプリ(C#)
- Windows ストアアプリの AppBar を作成してテストする


画面移動のたびに、画面が表示された時の処理を行うには、



