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

プロジェクトの中身を調べよう (3/6)

作成:2011-08-29 08:52
更新:2011-08-29 08:52

■MainPage.csとMainPage.g.i.csについて

続いて、MainPageのソースコードをチェックしましょう。「MainPage.cs」というファイルとして用意されています。これにデフォルトで設定されているソースコードを下のリスト欄(上のもの)に掲載しておきました。ざっとページのソースコードについてポイントを整理していきましょう。

1.名前空間について
プログラムはクラスとして定義されますが、これはアプリ名の名前空間に置かれます。「namespace PhoneApp1」というように名前空間が指定されていることがわかるでしょう。

2.クラスの定義について
ページに関する処理は「MainPage」という名前のクラスとして定義されています。これは、「PhoneApplicationPage」というクラスを継承して作られます。これがページクラスの特徴です。ページは常にこのクラスのサブクラスとして定義されます。

3.コンストラクタと初期化
ここで用意されている処理は、引数なしのデフォルトコンストラクタのみです。この中では、「InitializeComponent」というメソッドが呼び出されています。これはコンポーネントの初期化処理を行うものです。

デフォルトで用意されている処理としては、非常にシンプルですね。どうやらInitializeComponentで基本的な処理をしているようです。しかし、スーパークラスのPhoneApplicationPageを探してみても、実はInitializeComponentというメソッドは見つかりません。一体、どうなっているんでしょうか。

実は、このMainPageクラスは、これだけではないのです。C#に用意されているパーシャルクラスという機能を使い、クラスが分割されているのです。そして、自動生成されてプログラマに触れて欲しくないクラスと、プログラマがコードを追記するなどして利用するクラスに分けて用意されているのですね。

このMainPageクラスのもう1つのクラス(パーシャルクラス)は、「MainPage.g.i.cs(あるいはMainPage.g.cs)」というソースコードファイルとして作成されています。Solution Explorerを見ても、このファイルは表示されませんが、ちゃんと存在します。このソースコードを見てみると、下のリスト欄(下のもの)のようになっています。

ここでは、InitializeComponentメソッドの中で、MainPage.xamlファイルをロードしてGUIのコンポーネントを生成し、そこに配置したコントロール類のインスタンスをそれぞれ取得してフィールドに保管する処理が作成されています。

まぁ、今、ここでこれらのコードの意味をすべて理解する必要はありませんが、このように「MainPageクラスは、表示されないパーシャルクラスの部分で、レイアウトデータをロードし必要なコンポーネントをフィールドに保管する処理を行っているのだ」ということは知っておくとよいでしょう。

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

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

●プログラム・リスト●

・MainPage.xaml.csのソースコード

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.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneApp1
{
	public partial class MainPage : PhoneApplicationPage
	{
		// Constructor
		public MainPage()
		{
			InitializeComponent();
		}
	}
}


・MainPage.g.i.csのソースコード

using Microsoft.Phone.Controls;
using System;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;
using System.Windows.Threading;

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;
		
		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")));
		}
	}
}
※関連コンテンツ

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