Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Phone_81_Development_for_Absolute_Beginners

.pdf
Скачиваний:
34
Добавлен:
10.02.2015
Размер:
18.77 Mб
Скачать

First, I’ll create a new folder called DataSource:

Then right-click to Add > New Item … in that folder.

In the Add New Item dialog, (1) I’ll add a Class (2) named DataSource.cs then (3) click the Add button:

Windows Phone 8.1 Development for Absolute Beginners – Page 270

First, I’ll create a simple Ritual class using the prop code snippet to create the autoimplemented properties. I’ll also add the proper using statement for the

ObservableCollection<T> using the Control + [dot] keyboard shortcut.

Second, I’ll flesh out the DataSource class adding a private field of type ObservableCollection<Ritual> called _rituals. I’ll create a new instance of

ObservableCollection<Ritual> in the constructor. As you may anticipate, the rest of the DataSource class will define methods that operate on this private field.

Windows Phone 8.1 Development for Absolute Beginners – Page 271

We’ll follow the pattern we’ve seen several times before now, creating a helper method called ensureDataLoaded() that will determine whether or not the _rituals collection contains instances of Ritual. If not, then we’ll call the (yet to be implemented) getRitualDataAsync() method.

We delegate the responsibility of retrieving data from a serialized file back into an object graph stored in the _rituals collection to the getRitualDataAsync() method. We’ve seen this code before when working with serialized types that we need to “re-hydrate” back into an object graph. Most of the heavy lifting is performed by the DataContractJsonSerializer and the ApplicationData.Current.LocalFolder.OpenStreamForReadAsync() method.

Windows Phone 8.1 Development for Absolute Beginners – Page 272

There are several things we’ll have to fix with this code, including the creation of a constant for the fileName which I’ll define near the top of the DataSource class:

… and I’ll also need to add various using statements to satisfy all of the class references:

Windows Phone 8.1 Development for Absolute Beginners – Page 273

Next, I’ll implement the corollary to the getRitualDataAsync(), the saveRitualDataAsync() method which will be delegated the responsibility of persisting / “de-hydrating” the object graph stored in the _rituals field to disk. Here again, most of the heavy lifting is performed by the DataContractJsonSerializer and the Application.Current.LocalFolder.OpenStreamForWriteAsync() method.

We will want to allow one of our pages to add new Rituals to the _rituals collection. Therefore, we’ll implement a public method that will do that:

Windows Phone 8.1 Development for Absolute Beginners – Page 274

Furthermore, we’ll want to retrieve the _rituals collection:

We have enough of the data model in place. Next we can focus on the AddRitual.xaml page. I’ll right-click the project name and select Add > New Item … to display the Add New Item dialog. I’ll (1) select a Blank Page, (2) name it AddRitual.xaml, and (3) click the Add button:

Before we begin work on the new page, we’ll need to be able to reference the DataSource class from the entire project. To do so, (1) in the App.xaml.cs, (2) I’ll create a public static field called

DataModel (3) which will necessitate the need to add a using statement to the DataModel namespace.

Windows Phone 8.1 Development for Absolute Beginners – Page 275

In the App() class’ constructor, I’ll create a new instance of DataSource().

Now, that I have a way to get at the DataModel, I’ll wire up the data context for the MainPage.xaml. Up to now, we’ve done this by creating an instance of the DataSource class and holding a reference to it in a public read only property that we bind to declaratively.

However, this time I decided to do something different. Here I’m calling

App.DataModel.GetRituals() to get an ObservableCollection<Ritual> and set the DataContext property of the page. Using this technique, there’s no additional work to be done in XAML.

Windows Phone 8.1 Development for Absolute Beginners – Page 276

In the MainPage.xaml, I add RowDefinitions, then the title TextBlocks:

Below that, I add an inner Grid containing an ItemsControl. This will give the Grid the binding behavior of a ListView. Notice that the ItemTemplate has a blue-squiggly line beneath it … that’s because we haven’t created the ItemTemplate yet:

Windows Phone 8.1 Development for Absolute Beginners – Page 277

Next, we’ll add the ItemTemplate called dataTemplate. We implement it as a child to the Grid in the Grid’s Resources property. It consists of a StackPanel that arranged two TextBlocks vertically. The TextBlocks are bound to the Name and Description of a given Ritual object:

We’ll add a Button control to the DataTemplate. We’ll utilize the Button later, but for now it’s just for layout purposes:

Windows Phone 8.1 Development for Absolute Beginners – Page 278

In order to add a new Ritual, we’ll need a CommandBar with a Primary Command Button. To begin, put your mouse cursor in the Page element (top or bottom) and in the Properties window, select the New button next to BottomAppBar:

This will create a CommandBar object as a child to the Page’s BottomAppBar property:

Windows Phone 8.1 Development for Absolute Beginners – Page 279

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]