Home > Articles > Programming > Windows Programming

This chapter is from the book

Getting Started in Visual Studio 2012

Visual Studio is the premiere tool for Microsoft developers building apps for the Web and Windows, and has been for quite a while. It provides project management for keeping your app’s source files together; integrated build, deployment, and launching support; HTML, CSS, JavaScript, graphics, and Windows Store app manifest editing and debugging; and a whole lot more. There are several editions of Visual Studio, but we’ll use Microsoft Visual Studio 2012 Express for Windows 8 (a.k.a. VS), which is available for free6 and includes everything you need to build, package, and deploy your Windows Store apps.

To show you Visual Studio 2012 in action, we’re going to need something more interesting to build than an app with a static message (no matter how inspirational it may be). Developers new to any platform seem to have canonical apps that they build: Computer science students build text editors, compiler writers build Pascal compilers, web programmers build blogs, and, for some reason, mobile platform developers build news readers. So, let’s build ourselves a little Really Simple Syndication (RSS) Reader and start from my favorite template: the Navigation App (as Figure 1.3 shows us doing).

Figure 1.3

Figure 1.3. Creating a Windows Store Navigation App in Visual Studio 2012

The Windows Store app project templates provided with Visual Studio 2012 are as follows:

  • Blank App: This is pretty much the smallest Windows Store app you can build with the correct manifest and graphics files that includes the Windows Library for JavaScript (a.k.a. WinJS). This is a good template for when you’d like to start from scratch and build up.
  • Grid App: This is a simple but complete Windows Store app with three pages, navigation support, and the Windows 8 look and feel. This is a good template for starting with a full app that you’d like to modify.
  • Split App: This is like the Grid App but with two different pages.
  • Fixed Layout App: This is just like the Blank App template except that it allows you to build an app in a fixed-size area, like a casual game at 1024 × 768, and let Windows scale it up or down for you, based on the available space.
  • Navigation App: This template is the core of both the Grid and Split App templates, except with a single blank page instead of a set of fully functioning pages. This template gives you the navigation support you often want in your apps, but it also lets you build up largely from scratch.

Running the Navigation App template produces a Visual Studio 2012 Windows Store app project file for JavaScript (.jsproj) along with nearly the same set of files used to create our first sample, as Figure 1.4 shows.

Figure 1.4

Figure 1.4. The files generated by the Visual Studio 2012 Windows Store Navigation App project template

The format and the contents of the package.appxmanifest file are the same as the .appxmanifest.xml file we’ve already seen, but the .appxmanifest extension allows the file to have a custom editor in Visual Studio 2012, as Figure 1.5 shows.

Figure 1.5

Figure 1.5. The Visual Studio 2012 Manifest Designer

The manifest editor gives you a much easier way to edit the metadata associated with your app than getting all of the angle brackets right in the raw XML file.

The other interesting artifact added to the project is the Windows Library for JavaScript SDK reference. This brings in a reference to WinJS, a set of JS libraries produced by Microsoft to bring together the web platform; that is, HTML5, JavaScript, and CSS, with WinRT to make for a productive app framework for Windows Store apps built with JavaScript. You’ll see a lot of both WinJS and WinRT in this book, but to get you started, take a look at the default.html file generated by the Navigation App template:

<!DOCTYPE html>
 <!-- default.html -->
<html>
<head>
  <meta charset="utf-8" />
  <title>RssReader</title>

  <!-- WinJS references -->
  <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
  <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
  <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

  <!-- RssReader references -->
  <link href="/css/default.css" rel="stylesheet" />
  <script src="/js/default.js"></script>
  <script src="/js/navigator.js"></script>
</head>
<body>
  <div id="contenthost" data-win-control="Application.PageControlNavigator"
    data-win-options="{home: '/pages/home/home.html'}"></div>

</body>

</html>

In the head section of the HTML, you’ll notice the link and script elements that reference the styles and JS files that provide the functionality of WinJS. Part of that functionality is parsing the data-win-control and data-win-options attributes on the contenthost div toward the bottom of the file.

The data-win-control and data-win-options7 attributes enable declarative controls in Windows Store apps, essentially turning the HTML div element into an instance of a PageControlNavigator control from the RssReader namespace defined with this project. The data-win-options attribute is a simple JavaScript Object Notation (JSON) object passed to the control at runtime as constructor arguments. This declarative syntax allows programmers to easily lay out their controls using either the text editor built into Visual Studio 2012 or, as we’ll soon see, using visual tools.

In the case of the PageControlNavigator control, what’s happening is that the default.html file is really just a host for one or more logical pages that are loaded as your users navigate from one page to another. And, as you can see in the options for the control, the first page to be loaded is homePage.html, which the Navigation App template also generates:

<!DOCTYPE html>
 <!-- homePage.html -->
<html>
<head>
  <meta charset="utf-8" />
  <title>homePage</title>

  <!-- WinJS references -->
  <link href="//Microsoft.WinJS.1.0/css/ui-dark.css""
    rel="stylesheet" />
  <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
  <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

  <link href="/css/default.css" rel="stylesheet" />
  <link href="/pages/home/home.css" rel="stylesheet" />
  <script src="/pages/home/home.js"></script>
</head>
<body>
  <!-- The content that will be loaded and displayed. -->
  <div class="fragment homepage">
    <header aria-label="Header content" role="banner">
      <button class="win-backbutton" aria-label="Back" disabled
        type="button"></button>
      <h1 class="titlearea win-type-ellipsis">
        <span class="pagetitle">Welcome to RssReader!</span>
      </h1>
    </header>
    <section aria-label="Main content" role="main">
      <p>Content goes here.</p>
    </section>
  </div>
</body>
</html>

The HTML in homePage.html is a little bit more complicated than in default.html because it provides a Back button, a title, and a section making it pretty clear where Microsoft recommends that you put your content. In addition, the generated HTML pulls in the homePage.js file, which is where you put the logic that governs how the home page for your app is going to function. The generated skeleton code looks like this:

 // home.js
(function () {
  "use strict";

  WinJS.UI.Pages.define("/pages/home/home.html", {
     // This function is called whenever a user navigates to this page.
     // It  populates the page elements with the app's data.
    ready: function (element, options) {
       // TODO: Initialize the page here.
    }
  });
})();

The code inside homePage.js is wrapped in a self-executing, anonymous function, which is a JavaScript trick to keep everything in the function from leaking into global scope, providing the JavaScript equivalent of a private module. The "use strict" string is the JavaScript way of adding extra error checking at runtime, which is another good practice.8

Inside the module, the skeleton code provides a definition of a page control based on the ready function and the path to the HTML file associated with the page. A WinJS control is a reusable set of UI and behavior, whereas a page control is a control created around a logical page of HTML. The navigation support in the Windows Store app templates simply loads and unloads page controls as the user navigates between pages.

The ready event is fired when the page control is added to the HTML Document Object Model (DOM) and it’s an excellent place for us to show a list of feeds for our RSS Reader:

 // home.js
...
 // define the feeds
window.feeds = [
  { title: "Brandon Satrom",
    url: "http://feeds.feedburner.com/userinexperience/tYGT" },
  { title: "Chris Sells",
    url: "http://sellsbrothers.com/posts/?format=rss" },
  { title: "Channel 9",
    url: "http://channel9.msdn.com/Feeds/RSS" },
];


WinJS.UI.Pages.define("/pages/home/home.html", {
  ready: function (element, options) {
     // show the feeds
    var section = element.querySelector("section");
    section.innerHTML = "";

    feeds.forEach(function (feed) {
      var div = document.createElement("div");
      div.innerText = feed.title;
      section.appendChild(div);
    });
  }
});
...

The ready function is passed the div that presents the page in the HTML DOM via the element argument, so it’s a good place to do a query for the section element to hold our list of feeds. The code inside the ready function is standard HTML DOM manipulation code using the global feeds data defined above the function.

Running the app provides a full-screen Windows Store app that looks like Figure 1.6.

Figure 1.6

Figure 1.6. A list of feed titles in a Navigation App template project

If, in the process of developing this slightly functional app, you find yourself with issues, you can debug your app using Visual Studio 2012 by choosing Debug | Start Debugging, which gives you the following debugging tools:

  • Debugger: Set breakpoints, use the various step debugger commands, and watch JavaScript data and behavior.
  • JavaScript Console: Interact with JavaScript objects at a command line.
  • DOM Explorer: Dig through the HTML DOM and see styles by element.
  • Call Stack: Drill into the current JavaScript call stack.
  • Exceptions dialog: Turn on the option to break when a JavaScript runtime exception is thrown.

In addition to debugging your app on the local machine (which is the default), you have two other options: remote machine and the simulator. You can change these options by choosing Project | Properties and selecting the debugger to launch, as Figure 1.7 shows.

Figure 1.7

Figure 1.7. Choosing to debug against the local machine, the simulator, or a remote machine

The idea of remote machine debugging is that you can develop on a high-powered developer machine but debug on a more modest consumer-grade machine, like a tablet. This is handy to make sure your app works well on the type of machine you’re targeting.

The simulator option, on the other hand, creates a remote desktop session back to the machine on which you’re already running, providing a frame that lets you simulate various resolutions, landscape/portrait rotations, and touch, even if you’re not using a touch-capable device. Figure 1.8 shows our sample app running in the simulator.

Figure 1.8

Figure 1.8. A Windows Store app running in the simulator

And, as if that weren’t enough, Visual Studio 2012 is not the only tool you get when you install Visual Studio 2012 Express for Windows 8. If you’d like a WYSIWYG design experience for the visual portion of your app, you’ve got Microsoft Blend for Visual Studio 2012 (a.k.a. Blend).

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020