Introduction to Advanced Windows Store App Development using HTML5 and JavaScript
- 10/15/2013
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
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.
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
Correct answer: C
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.
Incorrect: The InternetAvailable event fires when an Internet connection becomes available. It does not tell the application about changes in the network state.
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.
Incorrect: NetworkStateChange is the correct event, but the value of true for the oneShot parameter fires the event just one time.
Correct answers: A, B, C
Correct: The task has to be declared in the application manifest.
Correct: The task can be created by the BackgroundTaskBuilder class.
Correct: A trigger must be set to inform the system on the events that will fire the task.
Incorrect: There is no need to use a toast to enable a background task. You can use it, but this is optional.
Correct answer: C
Incorrect: There is no specific task in the Windows Runtime library to fire a task just once.
Incorrect: Every task can be scheduled to run just once.
Correct: Many triggers offer a second parameter in the constructor to enable this feature.
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
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.
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
Correct answer: B
Incorrect: You can schedule a task to run every 15 minutes.
Correct: Fifteen minutes is the internal clock frequency. You cannot schedule a task to run at a lower interval.
Incorrect: You can schedule a task to run every 15 minutes.
Incorrect: You can schedule a task to run every 15 minutes.
Correct answer: A
Correct: All assigned conditions need to be met to start a task.
Incorrect: All assigned conditions must return true.
Incorrect: All assigned conditions need to be met to start a task.
Incorrect: There is no difference on condition evaluation whether the device is on AC or DC power.
Correct answer: B
Incorrect: The application has no reference to background task threads.
Correct: You need to implement the OnCanceled event that represents the cancellation request from the system.
Incorrect: There is no request to abort the thread during cancellation request.
Incorrect: The system can make a cancellation request.
Correct answers: B, D
Incorrect: This class has no methods to download files.
Correct: To download small resources, the HttpClient is the preferred class.
Incorrect: The BackgroundTransfer is a namespace.
Correct: The BackgroundDownloader is the class to request a lengthy download operation.
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
Correct answer: A
Correct: A class must be sealed.
Incorrect: There is no need to mark the class as abstract.
Incorrect: There is no interface required.
Incorrect: Answer choice A is correct.
Correct answer: C
Incorrect: You do not have access to all the .NET 4.5 classes since they simply are not available for a Windows Store app.
Incorrect: You cannot access C++ classes directly.
Correct: You can access WinRT classes.
Incorrect: WinMD library can be written in C# and Visual Basic, not only in C++.
Correct answers: A, B, D
Correct: A WinJS app can access C++ classes because they are wrapped in a WinMD library.
Correct: A C++ app can access C# classes because they are wrapped in a WinMD library.
Incorrect: A WinMD library cannot be written in JavaScript.
Correct: Answer choice C is incorrect.