libro
www.tuyano.com
初心者のためのWindows Phoneプログラミング入門

基本コントロールによるGUI作成 (4/5)

作成:2011-09-05 08:53
更新:2011-09-05 08:53

■コントロールのインスタンスはどこにある?

ところで、作成したソースコードを見て、ちょっと不思議に思った人はいないでしょうか。先のサンプルでは、textBox1.TextというようにしてtextBox1のテキストを取り出していました。――この「textBox1」って、何でしょう? なんでこんなものが使えるようになっているのでしょうか?

その秘密は、前回、プロジェクトのソースコードファイルを紹介したのを思い出してもらえれば解けるでしょう。前回、MainPageクラスは、実は2つの部分にわかれているのだ、ということを説明しました。「MainPage.g.i.cs」というソースコードファイルにMainPageのもう1つのクラス(パーシャルクラス)が用意されていたのでした。

下のリスト欄に、今回修正した状態でのMainPage.g.i.csソースコードを掲載しておきます。追加したコントロール関係のフィールドが増えていることがわかるでしょう。この部分ですね。
internal System.Windows.Controls.TextBox textBox1;
internal System.Windows.Controls.Button button1;
internal System.Windows.Controls.TextBlock textBlock1;
そして、これらのフィールドにインスタンスを設定する処理がInitializeComponentメソッドに追記されています。この部分です。
this.textBox1 = ((System.Windows.Controls.TextBox)
        (this.FindName("textBox1")));
this.button1 = ((System.Windows.Controls.Button)
        (this.FindName("button1")));
this.textBlock1 = ((System.Windows.Controls.TextBlock)
        (this.FindName("textBlock1")));
FindName」というメソッドにより、引数で指定した名前のコントロールを示すインスタンスが取得されることがわかります。これを、あらかじめ用意したフィールドに設定することで、textBox1といったインスタンスがそのまま利用できるようになっていたのです。

これらコントロールを扱うオブジェクトは、System.Windows.Controls.TextBoxというように、System.Windows.Controlsパッケージにクラスが用意されています。MainPage.xamlに配置した部品は、ここにあるコントロールクラスのインスタンスとして組み込まれており、それをFindNameで取り出して操作しているというわけです。

こうした、表からは見えないところでの仕掛けにより、ごく簡単にコントロールのオブジェクトをコード内から利用できるようになっていたのですね。
(by. SYODA-Tuyano.)

※プログラムリストが表示されない場合

AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。

●プログラム・リスト●

※MainPage.g.i.csの内容

using ……略……

namespace PhoneApp1 {
	
	public partial class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage {
		
		internal System.Windows.Controls.Grid LayoutRoot;
		internal System.Windows.Controls.StackPanel TitlePanel;
		internal System.Windows.Controls.TextBlock ApplicationTitle;
		internal System.Windows.Controls.TextBlock PageTitle;
		internal System.Windows.Controls.Grid ContentPanel;
		internal System.Windows.Controls.TextBox textBox1;
		internal System.Windows.Controls.Button button1;
		internal System.Windows.Controls.TextBlock textBlock1;
		private bool _contentLoaded;
		
		[System.Diagnostics.DebuggerNonUserCodeAttribute()]
		public void InitializeComponent() {
			if (_contentLoaded) {
				return;
			}
			_contentLoaded = true;
			System.Windows.Application.LoadComponent(this, 
					new System.Uri("/PhoneApp1;component/MainPage.xaml", 
					System.UriKind.Relative));
			this.LayoutRoot = ((System.Windows.Controls.Grid)
					(this.FindName("LayoutRoot")));
			this.TitlePanel = ((System.Windows.Controls.StackPanel)
					(this.FindName("TitlePanel")));
			this.ApplicationTitle = ((System.Windows.Controls.TextBlock)
					(this.FindName("ApplicationTitle")));
			this.PageTitle = ((System.Windows.Controls.TextBlock)
					(this.FindName("PageTitle")));
			this.ContentPanel = ((System.Windows.Controls.Grid)
					(this.FindName("ContentPanel")));
			this.textBox1 = ((System.Windows.Controls.TextBox)
					(this.FindName("textBox1")));
			this.button1 = ((System.Windows.Controls.Button)
					(this.FindName("button1")));
			this.textBlock1 = ((System.Windows.Controls.TextBlock)
					(this.FindName("textBlock1")));
		}
	}
}
※関連コンテンツ

「初心者のためのWindows Phoneプログラミング入門」に戻る