Windows 8 and HTML Part 10: Settings

9/10/2012 3:56:34 PM

Continuing with Part 10 of building WinRT applications with HTML and JavaScript.  You can find earlier installments at their respective links: Part 1Part 2Part 3- Part 4- Part 5- Part 6- Part 7- Part 8- Part 9. If you are interested in the source code for the TweetScan application, you can get it off of GitHub.  Make sure you sign up for How to develop a Windows 8 app in 30 days from Generation App.

Get video here.

Our sample app, TweetScan, defaults to using #Windows8 as the default search term when you launch the application from the Start page.  Part 8 showed how we could start with a new search term by integrating with the Search contract, but what about setting a new default search team from the Start page.  Clearly, we need a way to specify settings for our application.

Windows 8 provides an integrated Settings experience via the Charms bar that should be used by all applications.  When a user opens the Charms bar, they will see the Setting charm at the bottom of the bar.  Clicking on the Settings charm opens the Settings Flyout.  Without any customization, the settings flyout only displays an option for viewing the application’s permission set.  We can update the settings flyout to display custom options that we can use to provide a place for users to change settings for our application.

We hook our application into the settings flyout with the following code inside the activated event handler inside default.js

WinJS.Application.onsettings = function (e) {
   e.detail.applicationcommands = 
      { "preferences": { title: "Preferences", href: "/pages/settings/preferences.html" } };
   WinJS.UI.SettingsFlyout.populateSettings(e);
};

This registers an entry on the settings flyout that will show up as a link called Preferences.  When someone clicks that link, it will open preferences.html in the settings flyout.  You can specify more than one entry in the flyout by adding additional applications commands.

Prefences.html will contain all of the markup for a settings.

<div data-win-control="WinJS.UI.SettingsFlyout" 
       data-win-options="{settingsCommandId:'preferences', width:'narrow'}">
        <div class="win-ui-dark win-header">
               <button type="button" onclick="WinJS.UI.SettingsFlyout.show()" class="win-backbutton"></button>
               <div class="win-label">Preferences</div>
        </div>                
        <div class="win-content ">
          <div class="win-settings-section">
              <h3>Default Search Tag:</h3>
              <input id="tag" type="text">
              <button id="save" type="button">Save</button>          
          </div>
     </div>
</div>

It’s a very simple example – a text box to take the new default search tag and a button to commit the changes.  Not that the root <div> is a WinJS control – WinJS.UI.SettingsFlyout

In preferences.js, loaded by preferences.html when it is displayed in the settings flyout, the following code is added to the page’s ready event handler:

 var defaultTag = Windows.Storage.ApplicationData.current.roamingSettings.values["tag"];
 
if (defaultTag === undefined)
    defaultTag = "Windows8";
 
tag.value = defaultTag;
 
 save.onclick = function (e) {
    Windows.Storage.ApplicationData.current.roamingSettings.values["tag"] = tag.value;

Essentially, we check the roaming settings for a tag value (what the video for a more in-depth discussion of roamingSettings, please see the video).  If we have one, we set the text box with the tag value, otherwise we default to “Windows8”.  If the user click’s the save button, we update the setting value.

In home.js, we update the ready event handler to check for a default search setting.

var tag = Windows.Storage.ApplicationData.current.roamingSetting.values["tag"];
 
if (tag === undefined)
     tag = "Windows8";

Tags:

HTML | Windows 8

Comments

9/20/2012 5:45:51 AM

The source code cannot be accessed as the Github link mentioned in the article is broken. Can you please share the corrected link? Thanks!

mvark

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

About the author

Jeff Brand Jeff Brand

This is the personal web site of Jeff Brand, self-proclaimed .NET Sex Symbol and All-Around Good guy. Content from my presentations, blog, and links to other useful .NET information can all be found here.

E-mail me Send mail


Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

My Twitter Updates

XBOX
Live

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2013

Sign in