101 Windows Phone 7 apps Reading Notes-baby milestones

Source: Internet
Author: User

Course Content

Reading and Writing Images

Ø serialization

Ø bidirectional data binding

Baby milestones notifies parents of critical milestones in the development of infants from birth to age 2. This applicationProgramThis allows parents to track development milestones and ensure that their babies grow normally. It displays the skills that the baby can complete at each stage by month, so that the parents can record the date on which the baby gets the skill. The home page of the application shows the current growth data list of the baby every month.

The additional features of this application are mainly explained in this chapter. It shows how to store, retrieve, and display images in an isolated bucket. In this application, the monthly list (from 1 to 24) supports custom images as the page background. The main idea is that parents can take photos of their babies at the right time, provides nostalgic content for each list.

The main page

As shown on the home page 23.1, it contains a list box control, which can be used to link to the list of 24 months. Each label in the list is accompanied by a progress bar, which shows the progress of the current month. The completed month is displayed in the foreground color of the photo, while the unfinished month is highlighted in the photo.

Figure 23.1 progress bar converts a simple list box into a useful panel View

Note:

Using the settings defined in the following two settings. CS, Data. Ages displays a list of 24 stages that contain a series of skills.

Refer to the XAML of this pageCodeThe progress bar in the data template is directly bound to the percentcomplete attribute of each age instance. However, to make each text block control have a proper foreground color, a custom value converter is used here. This application uses three value converters, which are described in the next section.

In the code behind the queue, the mainpage_loaded method ensures that the selection view shows the latest phase, especially if the baby has been more than 9 months, it makes users feel annoyed when they view them through the scroll bar every time. This is done through begininvoke, because the scroll bar of the list box is operated immediately after the data content is set, which may not work. We need to complete data binding before using this method to operate list box.

In the Windows Phone application, the list box most commonly used selectionchanged event (triggered only after the selected content changes, rather than clicking) it is not expected to appear here. Therefore, the listbox_selectionchanged method is used to clear the selected content. The same is true for continuous clicking on the same record.

Age and Skill

The restore age and skill classes both implement the inotifypropertychanged interface. When the attribute is changed, the propertychanged event is triggered, just like the data source in data binding. This allows the record to be displayed on the home page and updates the details page (described in the next section) without manual operations.

Because the percentcomplete attribute in the age class is based on each date field in the skill list (null means incomplete, and any date indicates completed, it is more appropriate to trigger the propertychanged event for percentcomplete at the appropriate time. The age class can subscribe to the propertychanged event for each skill instance and trigger the event as percentcomplete when the date changes. On the contrary, the age class only requires the user to call refreshpercentcomplete when the related date changes.

The shortskill class has an explicit default constructor because it needs to be serialized for the isolated bucket. Generally, the C # compiler generates an implicit default constructor. However, when defining non-Default constructors, We must explicitly define a default constructor (if needed ).

Serialization and isolated storage application settings

Each object placed in isolatedstoragesettings. applicationsettings (or an instance assigned to the settings class used in this book)-including the transitive closure of all its members-must be serialized. As described in the previous chapter, the contents of the dictionary are serialized as XML in the applicationsettings file. If data cannot be serialized, all data in the dictionary cannot be stored. This error may occur in an invisible way, unless we capture unprocessed exceptions in the debugger.

In most cases, meeting this requirement does not require additional work. So far, no application in this book needs special processing to ensure that their settings are serializable, including all basic data types (string, numeric values, datetime, etc ), including the list using these basic data types and the classes using these data types, they are all serializable.

However, sometimes we need to make sure that the stored data is described by serializable data types. We can simply add an explicit default constructor for implementation. Otherwise, we may need more time to change the data type or customize its attributes (such as datamember and ignoredatamember, which allow us to customize the serialization of classes ), we can use the ignoredatamember attribute to mark it and exclude it.

Avoid storing multiple references of the same object!

Although we can store multiple references to the same object in the dictionary of an isolated bucket, these references do not point to the same instance during the next running of the application. This is because when each application is serialized, its data is stored as an independent backup. During deserialization, each data backup becomes an instance of different objects.

This is why baby milestones uses currentageindex settings instead of storage age instance reference settings. After serialization and deserialization, the logic of rolling list box can no longer be used, because the age instance is no longer in list box.

We can add user-defined logic to serialization and deserialization by marking some custom attributes in system. runtime. Serialization namespace: onserializing, onserialized, ondeserializing, and ondeserialized. To make the methods we mark called at the right time, they must be public (or contain an appropriate internalsvisibleto attribute) and have a streamingcontext parameter.

The details page

As shown in details page 23.2, it appears when the user clicks an age on the home page. This page displays a list of age-related skills. Click it to record the date on which the skills are obtained. After clicking, A Date Picker initialized to the current day is displayed, as shown in Figure 23.3.

Figure 23.2 shows the details page of the first month list

Figure 23.3 click the detail page after the first record

Note:

The visibility and text block of the Date Picker in each record of the worker are based on the date attribute value in the skill instance. This is done through two value converters.

The values of sorted Date Picker use two-way data binding, which is useful for users who control attribute values. The changes to the date attribute in the skill instance are not only automatically displayed in the Date Picker, but also the changes made to the Date Picker through the UI are automatically returned to the date attribute.

The custom isolatedstoragehelper class is used to load, save, and delete image files. As shown in figure 23.4, the image is selected by Photo chooser, which returns the selected image as a data stream.

Figure 23.4 photo chooser supports selecting an image from the media repository or taking a new image using a camera

Notes about isolatedstoragehelper

The deletefile method is the same as the code used to delete files in the previous chapter. The SaveFile method does not specify images, but stores the input binary stream as a new file stream. The image-related part is in LoadFile. It calls picturedecoder. decodejpeg (in the Microsoft. Phone namespace) to convert the stream to imagesource, so that it can be set to the source of the image or imagebrush.

The includecodejpeg method is quite slow and must be called in the UI thread. Therefore, this class caches all the imagesources it creates so that the next time its file name is passed to LoadFile, can return quickly (the same imagesource instance can be shared by multiple UI elements, so reusing it will not pose a risk ).

In addition to picturedecoder. decodejpeg, you can consider using writeablebitmap. loadjpeg. The latter can be called in the background thread to avoid the response delay caused by decoding a large image. Writeablebitmap will be introduced in "Jigsaw Puzzle" in Chapter 42nd.

LoadFile can use an alternative method to construct an imagesource by using images in an isolated bucket. It can use the default constructor to construct bitmapimage, and then call the setsource method to receive the stream of an isolatedstoragefilestream instance.

If our app allows you to save an image from the camera, it is a good idea to let users save it to the library. In this way, even if the application is uninstalled, the image is still stored on the device. In addition, once an image enters the media repository, You can synchronize it to your computer or share it in multiple ways (such as uploading it to Facebook or SkyDrive ). We can simply call medialibrary. savepicture.

The overloaded decodejpeg has the maxpixelwidth and maxpixelheight parameters. They can crop images based on performance requirements. However, when the width of a JPEG image is greater than the height, decodejpeg obfuscated the two parameters. It uses maxpixelwidth to limit the height and maxpixelheight to limit the width.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.