Home > Sample chapters > Programming > HTML, JavaScript and web development

Introduction to Advanced Windows Store App Development using HTML5 and JavaScript

Answers

This section contains the solutions to the thought experiments and the answers to lesson review questions in this chapter.

Objective 1.1: Thought experiment

  1. Your application, when not used by the user, is put in a suspended state by the Windows Runtime and, if the system needs resources, can be terminated. This is the most common problem that might explain why the app sometimes does not clean all data. The application cannot rely on background threads to perform operations because the application can be terminated if not in the foreground.

  2. To solve the problem, you need to implement a background task using the provided classes and register the task during application launch. Because the operations are lengthy, it is important to use a deferral. Do not forget to define the declaration in the application manifest.

Objective 1.1: Review

  1. Correct answer: C

    1. Incorrect: You cannot schedule a time trigger every minute. The minimum frequency is 15 minutes. Moreover, polling is not a good technique when an event-based technique is available.

    2. Incorrect: The InternetAvailable event fires when an Internet connection becomes available. It does not tell the application about changes in the network state.

    3. Correct: NetworkStateChange is the correct event. The false value for the oneShot parameter enables the application to be informed every time the state of the network changes.

    4. Incorrect: NetworkStateChange is the correct event, but the value of true for the oneShot parameter fires the event just one time.

  2. Correct answers: A, B, C

    1. Correct: The task has to be declared in the application manifest.

    2. Correct: The task can be created by the BackgroundTaskBuilder class.

    3. Correct: A trigger must be set to inform the system on the events that will fire the task.

    4. Incorrect: There is no need to use a toast to enable a background task. You can use it, but this is optional.

  3. Correct answer: C

    1. Incorrect: There is no specific task in the Windows Runtime library to fire a task just once.

    2. Incorrect: Every task can be scheduled to run just once.

    3. Correct: Many triggers offer a second parameter in the constructor to enable this feature.

    4. Incorrect: You can create different tasks to be run once. For example, a task based on network changes can run just once.

Objective 1.2: Thought experiment

  1. A background task has network and CPU quotas. Tasks have to be lightweight and short-lived, and they cannot be scheduled to run continuously. You have to rely on the specific class of the BackgroundTransfer namespace to upload and download files in the background.

  2. To solve the problem, you need to use the BackgroundUploader class and declare the use of the network in the application manifest.

Objective 1.2: Review

  1. Correct answer: B

    1. Incorrect: You can schedule a task to run every 15 minutes.

    2. Correct: Fifteen minutes is the internal clock frequency. You cannot schedule a task to run at a lower interval.

    3. Incorrect: You can schedule a task to run every 15 minutes.

    4. Incorrect: You can schedule a task to run every 15 minutes.

  2. Correct answer: A

    1. Correct: All assigned conditions need to be met to start a task.

    2. Incorrect: All assigned conditions must return true.

    3. Incorrect: All assigned conditions need to be met to start a task.

    4. Incorrect: There is no difference on condition evaluation whether the device is on AC or DC power.

  3. Correct answer: B

    1. Incorrect: The application has no reference to background task threads.

    2. Correct: You need to implement the OnCanceled event that represents the cancellation request from the system.

    3. Incorrect: There is no request to abort the thread during cancellation request.

    4. Incorrect: The system can make a cancellation request.

  4. Correct answers: B, D

    1. Incorrect: This class has no methods to download files.

    2. Correct: To download small resources, the HttpClient is the preferred class.

    3. Incorrect: The BackgroundTransfer is a namespace.

    4. Correct: The BackgroundDownloader is the class to request a lengthy download operation.

    5. Incorrect: The BackgroundUploader class cannot download files.

Objective 1.3: Thought experiment

You can choose a traditional C# or Visual Basic library that is perfectly suited to the need to be reused by other applications. But a traditional library cannot be reused by applications written in other languages.

If you opt for a WinMD library, you can reuse the exposed features in applications written in other languages. Moreover, you can give the library functionalities that work in the background using background tasks.

Creating a WinMD library has no drawbacks. You just have to follow some simple set of requirements:

  • The fields, parameters, and return values of all the public types and members in your component must be WinRT types.

  • Public structures may not have any members other than public fields, and those fields must be value types or strings.

  • Public classes must be sealed. If your programming model requires polymorphism, you can create a public interface and implement that interface on the classes that must be polymorphic. The only exceptions are XAML controls.

  • All public types must have a root namespace that matches the assembly name, and the assembly name must not begin with “Windows.”

Objective 1.3: Review

  1. Correct answer: A

    1. Correct: A class must be sealed.

    2. Incorrect: There is no need to mark the class as abstract.

    3. Incorrect: There is no interface required.

    4. Incorrect: Answer choice A is correct.

  2. Correct answer: C

    1. Incorrect: You do not have access to all the .NET 4.5 classes since they simply are not available for a Windows Store app.

    2. Incorrect: You cannot access C++ classes directly.

    3. Correct: You can access WinRT classes.

    4. Incorrect: WinMD library can be written in C# and Visual Basic, not only in C++.

  3. Correct answers: A, B, D

    1. Correct: A WinJS app can access C++ classes because they are wrapped in a WinMD library.

    2. Correct: A C++ app can access C# classes because they are wrapped in a WinMD library.

    3. Incorrect: A WinMD library cannot be written in JavaScript.

    4. Correct: Answer choice C is incorrect.