Windows Phone 7用のアプリケーションを最初から最後まで作成します。 パート11.データアクセスクラスを使用したデータの保存方法

前の部分

ほとんどの場合、アプリケーションのデータは外部データソースから取得されます。 アプリケーションでは、何らかの方法でこのデータを保存およびロードする必要があります。

このパートでは、次のことを学習します。

データ型


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

注:
保存およびロード操作に時間がかかる場合は、データをできるだけ早く保存し、できるだけ遅くロードする必要があります。 ただし、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 .
  1. 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 .
  2. 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 .
  3. 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 .
  4. 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 .
  5. 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 .
  6. 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 .
  7. 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 .
  8. 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 .
  9. 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 .
  10. 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 .
  11. 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 .
  12. 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 .
  13. 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 .
  14. 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 .
  15. 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メソッドで表されます。
  1. 保護された オーバーライド void OnNavigatedTo(NavigationEventArgs e)
  2. {
  3. base .OnNavigatedTo(e);
  4. CarHeader.DataContext = CarDataStore.Car;
  5. // ...その他のコード...
  6. }
*このソースコードは、 ソースコードハイライターで強調表示されました。

次の部分

Source: https://habr.com/ru/post/J118420/


All Articles