ほとんどの場合、アプリケーションのデータは外部データソースから取得されます。 アプリケーションでは、何らかの方法でこのデータを保存およびロードする必要があります。
このパートでは、次のことを学習します。
- Windows Phoneでサポートされているデータの種類。
- Windows Phoneの長期データストレージに使用するもの。
- データアクセスクラスを使用してデータを保存およびロードする方法。
データ型
Windows Phoneはさまざまな種類のデータをサポートしています。 データには、ページレベルのデータ、アプリケーションレベルのデータ、ユーザー入力データ、静的データ、インターネットから受信したデータの種類があります。 さらに、これらは読み取り専用であるか、現在のアプリケーションセッションの一時的なものであるか、すべてのアプリケーションセッションのストレージに格納されている場合があります。 アプリケーションのデータはさまざまな場所に保存できます。 アプリケーションは、次の場所からデータをダウンロードできるだけでなく、最初のもの以外のすべてにデータを保存できます。
データの場所 | 説明 | 例 |
---|---|---|
リソースファイルとコンテンツファイル | アプリケーションパッケージに含まれている読み取り専用ファイル。 リソースファイルは、それらを含むDLLを読み込むときに自動的に読み込まれます。 コンテンツファイルは、アクセスするとロードされます。 | 州都または背景画像のリスト。 |
インメモリ状態辞書 | アプリケーションが非アクティブ化されている間、短期のデータストレージに使用される一時ストレージ。 一時ストレージは高速ですが、その中のデータは数分後に削除されるため、信頼できません。 追加情報は、「非アクティブ化後のアプリケーションの復元」の部分で提供されます。 | 現在フォーカスされているコントロール。 |
分離ストレージ | 電話機のファイルシステムを表すストレージ。アプリケーションセッション間でのデータの長期保存に使用されます。 サンドボックスストレージは低速ですが、信頼性があります。 | ユーザーが選択した背景色。 |
リモートストレージ | 複数のアプリケーション間または同じアプリケーションの複数のインスタンス間でデータを交換するために使用されるクラウドまたはインターネット上のストレージ。 リモートストレージには低速の非同期アクセスがあり、使用できないこともありますが、最も柔軟性が高くなります。 リモートデータストレージは、この記事の範囲外の非常に広範なトピックです。 | ワインのリストを返すWebサービス。 |
注:
保存およびロード操作に時間がかかる場合は、データをできるだけ早く保存し、できるだけ遅くロードする必要があります。 ただし、LaunchingおよびActivatedイベントハンドラーにデータをロードすることは避けてください。
データアクセスクラスの使用
データアクセスクラスにデータストレージおよび取得コードをカプセル化すると便利です。 たとえば、OnNavigatedToメソッドのオーバーロードでデータをロードする場合、各ページは同じデータアクセスクラスを使用できます。 このクラスは、最初の呼び出しでデータをロードし、後続の呼び出しごとにキャッシュされたコピーを提供できます。
FuelTrackerアプリケーションには、データを保存および取得するために必要なすべてのコードをカプセル化するCarDataStoreと呼ばれるデータアクセスクラスが含まれています。 Carプロパティと、データを操作するためのいくつかのメソッドが含まれています。

Fuel Trackerアプリケーションの各ページには、アプリケーションがストレージから受信する必要がある1つのCarオブジェクトからのデータが表示されます。 CarDataStore静的クラスは、次のコードスニペットに示すように、 Car静的プロパティを介してデータへのアクセスを提供します。
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
このコードにより、各ページにオーバーロードされたOnNavigatedToメソッドは、データが既にロードされているかどうかに関係なく、プロパティの値を取得できます。 たとえば、データマッピングセクションで説明したように、 FillupPageクラスは、ユーザーインターフェイス要素のDataContextプロパティをCarDataStore.Carプロパティの値に設定します。 これは、次のコードスニペットに示すように、オーバーロードされたOnNavigatedToメソッドで表されます。
*このソースコードは、 ソースコードハイライターで強調表示されました。
- 保護された オーバーライド void OnNavigatedTo(NavigationEventArgs e)
- {
- base .OnNavigatedTo(e);
- CarHeader.DataContext = CarDataStore.Car;
- // ...その他のコード...
- }
次の部分