Creating an application for the Windows Store requires forethought to a number of considerations – one of which is how will you want your app to appear when seen in different view states.

When your application is running full screen in Windows 8, it will be in either landscape or portrait orientation. When the orientation is landscape mode, two additional views are possible: snapped or fill. A snapped view is docked either on the right or left side of the screen, with a width of 320px. The fill view is the larger remaining portion of the screen when another app is in snapped view.

Note Snapped and fill options are only available when the screen resolution is a minimum of 1366 x 768 and supporting wide screen (16:9) aspect ratio.

When developing apps using JavaScript, you have two main ways to manage the view states in your apps: Programmatically via JavaScript, or declaratively with CSS3 Media Queries. It is recommended that you use media queries to manage view state changes. However, there are times when you will need to respond to a view state change in code, and that is going to be our focus in this post.

What is Your Current View?

The Windows.UI.ViewManagement namespace provides some classes and enumerations for managing an applications view. The ApplicationView class can be used to determine the applications current view state by calling the value property. The return type of value is numeric, and maps to the ApplicationViewState enumeration. Armed with both, the following code shows a helper method for returning a string with the current view state name:

The above code is useful for determining what the current view state is. What about being notified when a view state change occurs? One way to accomplish this is by responding to the resizing of the window as seen here:

The above line of code would allow you to write code in the anonymous function to query the applications view state and respond accordingly.

If you happen to be using Page Controls you are also likely using the PageControlNavigator, which is a class defined in a navigator.js file included in Visual Studio projects that support single-page navigation model. Let’s look under the hood of PageControlNavigator, particularly when it responds to the application resizing:

The above snippet of code shows what the PageControlNavigator does when it responds to resizing. The code checks to see if the current page control has defined an updateLayout function, and if it has, it calls it with parameters useful for interrogating. Now let’s look at an example of an updateLayout function inside a page control:

In the example above, an element with the ID of “pageTitle” sets it’s text to equal the response of the helper method shown previously – which returns a string with the current view state name.

Responding to a Different View

Now that we have established where to write code when the view changes, it is now time to focus on what you might do in that response. Suppose you have a ListView control on the page that displays items in full screen mode like so:

Now when going to snapped view, it looks like this:

Notice in snapped view the scroll bar is still moving horizontally. What if we want the list to scroll vertically when in snapped view? This is where a few lines of code can make the difference to improve the user experience.

The ListView has a layout property that allows you to change from a horizontal grid to a vertical list. When a view state change occurs, the following code conditionally switches the layout:

The code above (written inside the updateLayout function) changes a ListView control with an ID of “list” to switch to a different layout if the view state is snapped, otherwise it reverts to the original grid layout. Doing this along with some tweaks to CSS via media queries (such as adjusting margins) results in the improved UI when in snapped view:

The content can now be scrolled vertically!

Embrace a Different Point of View

No doubt you will enjoy being a user of applications running on Windows 8. One of the features that is sure to be used by multi-tasking enthusiasts is running apps in snapped & filled modes. Make sure you when you develop apps for the Windows Store, you create a compelling UI for each view. Also make sure to take advantage of all the resources available at Generation App!

CSS3 has brought much goodness to the web developer community. The feature that potentially steals the show is CSS3 Animations – which can also be used when developing applications for the Windows Store via JavaScript.

If you are not familiar with animations, the name itself is practically self-explanatory. Animations can deliver very appealing “eye-candy” in a variety of ways including object movement, resizing, transforming, fading, style changes, and much more. Because animations have been around awhile, there is plenty of information out there to help you get started, such as the animations topic at the IE 10 Guide for Developers and Hands On: Animations on IE Test Drive. Our focus here is more on how to use animations in the context of writing apps for the Windows Store.

Animations Simplified

A chronic worry when developing content for the web is how our creative work will render in multiple browsers. For example, the following CSS rules define an animation for bringing an element in from above the screen:

Why so much reduction? When writing Windows Store apps, we are not worried about supporting multiple browsers. Whatever standards work for IE 10 are all we need. Granted, if you used the previous rules with all the vendor prefixes while creating an app - no error would occur, they would just be ignored.

Playing Animations

Animations are triggered as soon as they are defined. In the previous example, as soon as the page with the element with and ID of “message” was navigated to, the animation was triggered. Animations can be triggered from code as well. Consider the following example:

The rules above introduced a slight change from what we observed previously – the animation was moved to a class named “animate-drop-in”. Assuming the element with an ID of “message” is not defined with the new class name, the animation will not occur. However, we can trigger the animation in code as seen here:

The click event handler for the “message” element toggles the class name. Once the app sees the element is assigned to an animation (which is what the new class does), it triggers the animation. The toggle method of classList returns a boolean value based on whether it added (true) the class name or removed (false) it.

If needed, you can respond to three events related to animations. Here is the syntax for defining the handlers for each:

The above event handlers are handy and can allow a chain of complex animations. For example, in the event handler for when an animation ends, you can add code to trigger a completely new animation.

Animations vs. Transitions

A close relative to animations is CSS3 Transitions, and those are covered in another post entitled CSS3 for Windows Store Apps: Transitions. Since both of these technologies offer “animation” as the effect, you might wonder when to use one or the other. With that in mind, take the following in consideration when trying to decide which is best:

Animations

Effect is played as soon as animation is defined, or programmatically

Multiple stages are possible, allowing more complex animations

Transitions

Effect is triggered when a targeted property value changes, usually due to a state change such as when a :hover is defined. A targeted property value changed in code will also trigger a transition.

Syntax is much simpler, thus, effect is usually simpler than what can be done with animations.

Another point to think about is what the intent of each technology is. Transitions (as the name implies) are really intended to provide a nice visual effect when something is changing state. Animations can be used for anything pertaining to the logic of the application.

With Great Power…

Animations offer a great way to enhance the user experience. However, too much of a good thing can become an annoyance. So be judicious with your implementations of animations.

It is an exciting time to be a developer with web skills. Harness what you know and use it to great some great applications for the Windows Store. Be sure to take advantage of all the resources available at Generation App!

Windows 8 brings in a new era of opportunity for the web developer/designer in that applications in the Windows Store can be written with “web” technologies such as HTML5, JavaScript, and CSS3. Creating a compelling user experience is an important aspect of a successful app, and CSS3 is a fantastic way to reach that objective. This post will specifically address a great way to enable fast and fluid interactions in your app by using CSS3 Transitions.

If you haven’t been using transitions or know what they even do, then let’s start with a quick description: Transitions allow content on the screen to change from one visual state to another in a gradual, animated way. To learn more about the basics of transitions, check out the Transitions topic at the IE 10 Guide for Developers, and play with the Hands On: transitions on IE Test Drive. The focus here will assume you know the fundamentals of transitions and want to how to use them when developing apps for the Windows Store.

Transitions from Web to App

Will you need to define transitions differently for apps than for web pages? Technically, the answer is no. But you may not need to work so hard. For example, consider the following CSS rule:

When developing for the web, we worry about web things – such as supporting multiple browsers. This is not a worry when developing an app for the Windows Store. This means you don’t have to riddle your CSS with all the vendor prefixes as seen above. Therefore the above rule is reduced and far more simple to read and manage as seen here:

.transit {
transition: all 0.4s;
}

When a transition is defined, it specifies what properties are targeted for animation and for how long. In the example above, the all keyword means all animatable properties. A transition is triggered when any targeted property value changes. A common scenario where values are changed is when using CSS pseudo classes such as :active, :checked, :disabled, :enabled, :focus, :hover, :invalid, and :valid are in play. Hovering is one of the most common uses of transitions, as seen here:

The above example fades from white to red and smoothly back again when hovering. Although hover effects are appealing, keep in mind that Windows 8 is an operating system designed to fully embrace touch screen devices. Hovering works well with a mouse, but not with a finger. One possible way to deal with this is seen here:

The subtle change above is adding the :active pseudo class if the targeted element is pressed down. This may not be practical in some circumstances, but it at least involves use touch devices when using you app.

Transitions in View States

Another way transitions are triggered is by use of media queries. Windows 8 applications can run in multiple layouts, and media queries can respond to layout changes by responding to the view state. Here is an example of a style sheet shell with media queries for each supported view state:

It is useful to know that transitions can be triggered by layout changes because proper use of them can enhance the fluidity of the application. For example, a switch to a snapped layout might require a change in font size. With just a subtle transition, you can get the effect of a graceful move to snapped view and back. This is demonstrated in the following CSS rules:

When the rules are implemented above, a switch in the application to a snapped position will create a simple shrink effect. When switching out of snapped view, the text quickly grows. Careful use and experimentation can lead to a polished user experience.

Transitions in Code

While changes to targeted properties of a transition can be defined in the CSS rules, another way to make property changes programmatically through JavaScript.

Transitions are always timed. One way to interact with a transition in code is to respond to the transitionend event when it is complete. To demonstrate, first look at the following CSS rule:

In the code above, an anonymous function is used as the event handler when the transition ends. Note that the argument supplied to the function has an elapsedTime property, allowing you to determine how long the transition lasted. Because the transition is triggered by the :active pseudo class, it will only reach 4 seconds if the user continues to press down on the element. The code above detects the user let go too soon, and changes the text of the element to encourage the user to keep pressing.

The code now responds if the transition reached a full 4 seconds. If that happens, it changes the text of the element to “Let Go…”. It also calls on the element’s classList property, and adds a new class named vanish. The addition of the class rule will invoke another transition that will make the element shrink and disappear in 1 second.

The classList property allows you to easily change the the state of the class attribute, which will invoke a transition if any targeted property values change. You can use classListfor adding, removing, inspecting, or toggling class names in the attribute. You can also simply change any of the elements style property values directly to also invoke a transition.

Transitions Beyond

Fans of CSS3 transitions will enjoy knowing that such powers are easily tapped when developing Windows Store apps with JavaScript. You should also be aware that the WinJS.UI.Animation API contains methods for transitioning content as well.

Have fun coding, and be sure to benefit from all the resources available to you at Generation App!

Are you confident you are writing your JavaScript code error free? Do you wonder if traditional JavaScript error handling techniques still apply when developing for Windows Store apps? Does the Windows Library for JavaScript provide additional support for error handling? Do you deal with errors differently when writing asynchronous code? This entire post is dedicated to addressing these questions!

To Err is Human

Let’s be clear – if you were perfect and could write code that never fails, you would not need to read any further. For the rest of us (which is all of us), we must acknowledge that errors very often creep into code. Furthermore when developing with a language that is not strongly-typed such as JavaScript, there is potential for even more errors. Why? Consider the following code:

var x = 4;
// somewhere else in code...
x = "four";

The above code is valid. It showcases the dynamic nature of the language. However, it also illustrates the potential confusion as to what the variable x is. Any misuse of x based on assumptions could lead to buggy behaviors. One way to minimize these kind of issues is to be consistent with variable assignments with regard to inferred type. In other words, if a variable is assigned to a string, keep it a string.

Syntax errors are typically reported through Visual Studio when running the application in debug mode. However, the error message may not clearly state what is needed for correction. For example, observe the following syntax error:

varr x = 4;

Isolated from all other lines of code, it is easy to see that the keyword var was misspelled. Yet the IDE reports the error at runtime this way:

An entry level scripter could debate that there is a ‘;’ at the end of the line, and rationalize the error message itself is erroneous. The moral of the story here is to examine the complete line of code and surrounding context for discrepancies when the reported error message is not clear.

For web developers who are bringing their JavaScript skills over to Windows Store app development, the use of try…catch…finally statements are fully supported and should be used when attempting to execute any line of code that is conditional to a successful outcome – such as accessing external resources.

Mistrust is a Virtue

I find it a little humorous that the uninitialized use of WinJS.log (a method for logging error information among other things) will cause an error to be thrown at runtime. The log method can be initialized with WinJS.Utilities.startLog (for logging to the JavaScript console) or assigned to a custom method with logging behaviors defined by the developer. A straight call to the log method without initializing will cause following:

Of course, when the log method is initialized - no errors are reported. However, consider a scenario where settings in the application determine whether the log method will be initialized or not. In that case, what is a safe way to call the log method? Some might create a global variable that could be used in an if statement to see if logging is enabled before every call. That will work, but there is another way. Consider the following code:

The second line of code is “safer” because it starts with a check to see if the log exists. The term “safer” is used instead of “safe” because by saying log “exists” what that really equates to is that it is not one of the following: null, undefined, an empty string, or a numeric value of zero. Even so, it is still safer to use the logical && operator (no if statements needed) because it will exit the statement immediately when a false value is returned.

There is another technique using the|| operator which can help reduce errors by producing a default value if one is not provided. In the following code, the name parameter is given a value if none were already assigned:

To prevent unwanted errors like this, there needs to be a guarantee that the method is safe to call. One way to do so is by using an approach mentioned previously - as seen here:

if (name && name.toUpperCase) {
// safely call name.toUpperCase();
}

At other times it may be more preferable to simply confirm what type it is. A common way to accomplish this is to use the typeof operator which returns a string declaring what the underlying type is as seen here:

The isTypeOf function first checks to see if the t parameter has a value, that the value has a charAt method, and that a call to charAt(0) will not return an empty string. If all conditions are true, the function returns a value based on the comparison of t and the type exposed from Object.prototype.toString.call. Note that if the t parameter is not provided or not a string, an error is thrown using the WinJS.ErrorFromName factory method which is an easy way to create an error object based on name (error type or category) and message.

So why throw an error? Doesn’t that defeat the purpose of preventing problems in the first place? In the above example, the error is thrown from inside a helper function. Because this function could end up in a reusable library, it is a methodology for communicating it is being misused to application level code. With that understanding, you will likely spend far more time handling errors in the application level code vs. throwing them.

The main message up to this point is to take preemptive strikes in your code to minimize errors as much as possible. Now what about the errors that went unhandled?

Leave Nothing Unhandled

For all the unknown errors that could happen in your application, there is a simple way to catch them all in one place. The WinJS.Application object has an onerror event that traps all unhandled errors in one place. Because error could happen at any time, this should be hooked to an event handler as soon as possible in the application lifetime. Most of the JavaScript project templates for the Windows Store contain a default.js file where application initializing occurs. Near the top of that file, you can add an event listener for unhandled application errors like seen here:

If an unhandled error happens anywhere in the application, the above listener function gets called. A message is displayed to the user indicating the details of what has occurred, and then the app stops. The app terminates because the error is still considered unhandled. Returning true from the error handler communicates to the system that the error has been handled and is ok to continue.

Unhandled errors can also be caught at the page level while the page is processing. This is especially easy to do when using WinJS.UI.Pages. The following code is an example of how to add an error event listener for the PageControl instance within the define method:

It is worth repeating that this event only fires if an unhandled error occurs during the initial processing of the page. Therefore, unhandled errors that occur in after the processing of a page (in a click event for example) will be directed up to the application level for inspection.

When Promises are Broken

Promises allow for asynchronous code, and because this introduces it’s own level of complexity, it is important to know how to handle errors when using them. In order to appreciate the different options available, consider the following example of a custom promise:

The doAsync function defined above is a custom promise. When called, it allows a message (provided into the function via the msg parameter) to be used after a number of seconds (determined by the limit parameter) passes by. When the time limit is reached, the “complete” function(represented by c parameter) is called. A “progress” function (represented by p parameter) is called every second, allowing the application to potentially update the UI. And if any errors occur, the catch block passes the error into an “error” function (represented by e parameter). If the value of msg equals “fail”, an error is thrown.

The following code demonstrates all the helper methods that will be used when calling the doAsync function:

Each function calls on WinJS.log to display data. Now here is the code that brings them all together:

doAsync("promise", 4) // doError not called
.then(doComplete, doError, doProgress);
// final call was to doComplete function
doAsync("fail", 4) // doComplete not called
.then(doComplete, doError, doProgress);
// final call was to doError function

In each call to doAsync above, a message is passed in, and the duration of the asynchronous call will be limited to 4 seconds. Each second that ticks by will cause the doProgress function to be called. If all goes well when the 4 seconds are up, the doComplete function is called. However, if the promise encounters an error, the doError function is called instead.

Promises can be chained together to allow one asynchronous call to invoke the next when completed. When chaining promises together, put the error handler only in the final link of the chain – the done method. Here is an example of chaining promises using the doAsync function:

In the promise chain above, when “chain(1)” is complete it calls on “fail”. Since “fail” will cause an error to be thrown, “chain(2)” is never called, and processing is directed to the doError function passed into the donemethod of the chain.

Promises can also be joined together too. This allows multiple asynchronous calls simultaneously, but allowing a method to be called when all the joined promises complete. When handling errors in this scenario, do the following:

Did you know you could use iFrames in WinRT apps developed using JavaScript? Is this suppose to be exciting? Why would you care? For the answers, follow along as I take a journey of discovery attempting to add live Twitter feeds to my app.

To set the stage for what I want to do, you must first understand what a Twitter Widget is. The folks at Twitter have made it real easy for web developers to create a custom, dynamic section of a web site to display tweets based on a user, a search, favorites, or a list. Once I know my tweet criteria, I can customize the appearance, dimensions, and other relevant options. When I am done with all my customizations, I can grab the code that will make the magic happen on my web site. Um… but I want this in my WinRT app. Will it work? Lets find out.

Now I will add this code to my project in Visual Studio 2012. I have just created this project using a blank template. In the default.html file, I will add the Twitter widget code I created above. Here is what my markup looks like:

Why did the script not execute? Because my default.html page is considered local in context to my application. The only way an external script would be allowed to execute is if it was executed in a web context. Well who decided those rules? Perhaps a better question for now is – How do I know if I am in local context or web context?

Any HTML file that is physically part of a project is considered as local in context. The converse to this is any HTML that resides externally or is remote to a project is considered to be in web context. How can I execute HTML remotely in my WinRT app?

Using iFrames

I can use an iframe in my HTML to point to an external web page. To demonstrate, I will remove the script I added to my default.html page (we will return to that code later), and replace the contents of the body tag with the following:

This will cause the iFrame to load the respective href value once either of the anchor tags are clicked.

Recommended: Create an HTML file in the root directory of your project namedmsapp-error.html. This file will be loaded automatically in the iframe when errors occur due source resources not loading or not found!

Now returning to my original objective, I would like to see the code I grabbed from Twitter work in my app still. I have an idea! I will take Twitter widget code and put in in a new HTML file in my project. I will name the file twitterframe.html, and create it in the root project directory. Here is the contents of that file:

Will I get my desired output? Can I trick the iframe to run a page in local context but treat it like it is in web context? Not the way I am doing it. When I run my application, I get the same error I received earlier. However, I was on the right track of thinking. By using a special moniker preceding the URL, I can ask for the local page to be executed in a web context. Here is the syntax for that:

Here is a simple JavaScript RegEx pattern for extracting any URL that begins with http:

<script type="text/javascript">
// example string, looks like a tweet to me :)var str="Awesome resources for HTML5! http://bit.ly/palermo4html5 #HTML5";
// UPDATED 2012-07 thanks to ArthurDent69!// could also be written as:// var regexUrl = new RegExp("https?:[^\s]+", "gi");var regexUrl=/https?:[^\s]+/gi;
// if a match is found, the return type is an array of matches
document.write(str.match(regexUrl));
// output of above line of code:// http://bit.ly/palermo4html5
</script>

In my previous post regarding a CSS Hover Trick, I was challenged in the twitter universe to do something similar with images, but with the click event. Could this be done without JavaScript? But of course. What makes this possible is use of two CSS3 selectors: :not, :target. This will not work in older browsers, so check out how to do feature detection in this post on detecting CSS3 selectors.

The code found below will make images appear based on what anchor tag was clicked without using any JavaScript! Here are screen captures to demonstrate the desired behaviors:

No anchor tags have been clicked

First anchor tag clicked

Second anchor tag clicked

Third anchor tag clicked Shameless self promotion

Here is the code to make it all work! To reproduce in your own environment, simply replace the images with your own!

In no way am I claiming this to be original. But I can’t say I have seen this trick done anywhere else. With the CSS :hover selector, you can create a nice “status message” appear in one location while hovering over particular items in a list (or menu). Below are the screen captures of what the trick accomplishes, followed by the entire source code to make it possible. Enjoy!