HTML5

setting view states in HTML windows store apps using Blend

Intro

From developing XAML applications I got used to working with the visual state manager to set various states of my applications and controls. In Windows Store apps that are build using XAML use the visual state manager to change various properties when the view state of the app changes. The app can be ran in fullscreen, filled or snapped state.

When building Windows Store apps using HTML and JavaScript I wanted to show some UI different when the view state changes. One way to accomplish this is to change the CSS class of the elements from JavaScript. Just handle the change in view states and call some methods to set the classes.

Another way comes very close to the visual state manager from the XAML world. And can be done from within Blend.

CSS Media queries

It turns out you can set the styles for the view states using CSS media queries or media types. You might know these from building websites, where they’re often used to set different styles for different media or screen sizes.

If you look at the Style Rules pane in Blend for Visual Studio, when having an HTML Windows Store app opened, you can see four different “@media” definitions. Each one is used for a different view states. The media queries are added by default, but if they are missing or you want to add another you can do this by right click on the .css file.

image

Notice that only the fullscreen-landscape is white. The other three are greyed out. This is an indication that Blend is showing the landscape orientation at this moment. You can change the current orientation used in Blend on the Device pane. Just select another option for view.

image

To demonstrate how to use this I created a very simple example showing a grid with 6 boxes. For this I used a FlexBox. When a FlexBox is filled with elements they are stacked horizontally or vertically and wrapping could be enabled.

image

html:

<div class="ms-flexbox">
    <div class="tile red"></div>
    <div class="tile green"></div>
    <div class="tile red"></div>
    <div class="tile green"></div>
    <div class="tile red"></div>
    <div class="tile green"></div>
</div>

css:

.ms-flexbox {
    display: -ms-flexbox;
    -ms-flex-flow: wrap column;
    width: 1000px;
}
.tile {
    width: 250px;
    height: 200px;
    margin: 20px;
}
.tile.red {
    background-color:red;
}
.tile.green {
    background-color:green;
}

A similar layout like this is often used in Windows Store apps. Unfortunately it does not work when the app is snapped. In the snapped state the elements should be shown in a list, but will remain in three columns at this point. You’ll need to place parts of the .ms-flexbox class in both @media queries. You could do this by hand by editing the .css file, but I’ll show you how to do it using Blend.

The first step is to copy the .ms-flexbox class to the fullscreen-landscape and to the snapped media queries. Just right-click the .ms-flexbox entry on the Style Rules pane and click copy. That paste it on the fullscreen-landscape and the snapped media queries.

image

image


 

 

Next, select the original .ms-flexbox class and have a look at the CSS Properties pane. There’s a long list of collapsible panels containing a lot of properties. To shorten the list to only the properties that have a value, check the “View set properties only” checkbox. 

image

The original .ms-flexbox style will be the style that is applied to all view states and contains the properties that are equal in every state. The Flexbox properties will be different. So these need to be removed. To do this click on the small advanced properties square next to Flexbox and select clear.

image

Select the .ms-flexbox style on the fullscreen media query on the Style Rules pane. For this part of the style clear the layout and the sizing. And do the same for the style on the snapped media query.

The style for the snapped state has to have some different properties for the Flexbox. Start by setting the View to the snapped state on the Device pane, so it visible what the change in properties is causing. First, clear the –ms-flex-wrap property, because in this state wrapping is not needed. Than, set the
–ms-flex-direction property to column. Notice the change in the designer.

image

And that’s it. Well… Almost actually. We haven’t done anything with the filled and fullscreen-portrait states. You could copy the state from the landscape state, but this would mean you’ll have to keep them in sync in case of changes.

At this point I changed the CSS by hand and added the filled and portrait definition to the landscape view state, which results to:

@media screen and (-ms-view-state: fullscreen-landscape),
screen and (-ms-view-state: fullscreen-portrait),
screen and (-ms-view-state: filled)
{
    .ms-flexbox {
        -ms-flex-flow: wrap;
    }
}

Wrap up

Handling view states is an important part of you Windows Store app. In a real world application the styles and HTML will be a lot more complex than I showed here, but I hope you’ll get the idea. Otherwise, just drop me a line and I’ll see if I can expand the tutorial.

SFX in WinJS

When working on my entry for the Js13kGames contest I was looking for a synthesizer or sound generator for HTML5. I came across a javascript port of an a synthesizer in actionscript. It’s build by David Humphrey. Markus Neubrand added the parameter strings. It is available on github. Due to the time limit I only got it to work in Firefox at the time, but the library works in Chrome too. Unfortunately Internet Explorer is not supported.

But, when porting the code to an Windows Store app I suddenly noticed that the sounds worked.

I’ve created a small module around the synthesizer and parameters to make it a little easier to play sounds. Depending on how often sounds are required you can have more channels to play sounds simultaneously. By default you can play 4 sounds at a time with my module. Initialize it as follows.

First create a new audio object and initialize it. This creates a couple of HTML5 audio elements, one for each channel. Than it initialized the synthesizer. Next, two sound effects are stored by providing a key to identify them and a string containing a lot of numbers. These numbers are the parameters for the synthesizer. More on this later.

var sound = new audio(); 
sound.init(); 
sound.storeSfx('smallexplosion', '3,,0.1094,0.6292,0.3374,0.1949,,0.0785,,,,,,,,,,,1,,,,,0.5'); 
sound.storeSfx('powerup', '1,,0.0202,,0.4455,0.4339,,0.2229,,,,,,,,0.5824,,,1,,,,,0.5');

To play a sound just call the playStoredSfx function providing the key of the sound to play.

sound.playStoredSfx('powerup');
sound.playStoredSfx('smallexplosion');

Now on that weird looking string. That string contains all parameters needed by the synthesizer to play sounds. Luckily for us you don’t have to guess these numbers, just use the actionscript synthesizer. Go to this URL: http://www.superflashbros.net/as3sfxr/  and just play with it to create your sound. When you have reached the point where you have a sound that you can use, just hit CTRL+C. This copies the sound to the clipboard in string format, precisely the way as the parameter string for the sound effect.

image

Just have fun with it and use it in your Windows Store apps.

You can download my module, including SfxrSynth.js and SfxrParam.js here.

One last thing. It turns out John Vinnik ported the synthesizer to Windows Phone too. You can’t copy the sounds to the clipboard of your pc with that, but it is fun to play with :)

http://www.windowsphone.com/en-us/store/app/sfxr-lite/b1792f23-fd56-e011-854c-00237de2db9e

WinJs DesignTime Mode

While working on the port of my game Orbizoid to Windows 8 I ran into an issue with the starfield in de background of the game. The starfield was running in Expression Blend too. At first this was kinda funny, but soon the performance dropped and it became unworkable. There used to be a property to detect if you are running inside Expression Blend before, I hoped this was still available when working on Windows 8 applications in javascript. I had to search for a moment, but it is possible.

var isInDesigner = Windows.ApplicationModel.DesignMode.designModeEnabled;

if(!isInDesigner){
// code not running in Expression Blend
}

now you can safely use the isInDesigner variable throughout your code to enable/disable specific code running in Blend.

Js13kgames contest

orbizoid - smallLast few weeks I worked on a an entry for the Js13kGames contest. The main goal of this contest was to write a game in javascript that was only 13k in size. I find that writing software for contests is a great way to learn more about a language. The tight deadline forces you to think and you will end up with a finished product. Because I’m working a lot with javascript lately, this contest was a great training.

I build the game Orbizoid. The objective of the game is to catch the colored stars that float around with moons matching the colors of the stars. The moons are at different positions around a planet you can move with the mouse or by touch. When choosing the hard mode of the game the moons orbit the planet with various speeds.

The game is developed entirely in Visual Studio 2012 on Windows 8. I tested the game in Internet Explorer 10 most of the time. Although I feel it runs the best in IE10, I tested it in all other major browsers. There’s not a lot of css in the game. I used the new page inspector a lot when debugging the css. The game is rendered on a HTML5 Canvas. Orbizoid in blendWell, actually the game is rendered on a copy of the canvas and is copied to the main canvas. To reduce the size of the game, it doesn’t use any image files. All graphics are rendered at the start of the game. In the first version the glow was made by rendering 5 different canvasses on top of each other. The only browser that withstood this was Internet Explorer 10. So I decided to prerender the glow into the images. Every image is rendered on a canvas and turned into an image by getting the base64 encoded bytes of the canvas.

The game loop started out as a timeout which called itself 60 times a second. I later optimized it by using the new window.requestAnimFrame function. I got the game running at 60 frames per second. Unfortunately not all browsers managed to get this frame rate constant. Therefore I delayed the game loop to run at 30 fps. To monitor the frame rate I used stats.js.

The mathematics of this are are fairly simple. I wanted to have some space-related background animation. A starfield was the most obvious. It’s just drawing circles on x/z and y/z. By lowering the z each frame the stars look like they move closer. When z comes below a certain point it is reset to the furthest. If anyone would like a more detailed description on this effect just drop me a line and I’ll write a detailed tutorial about it. The orbiting planets are sin and cos calculations that are update every frame. For the collision detection I just calculated the distance between stars, moons and planet. If this is below a the radii of the objects there’s a collision.

In the end I had a few Kbs left so I added some sound. Unfortunately the html5 audio API not implemented very well among the different browsers. I wanted to used the jsfxr library. I only got it to work in Firefox. But it adds a nice extra feature.

I’m currently working on the Windows 8 version of this game. When it’s done it will be my first game in the windows marketplace. I started by copy all code to a new Windows 8 javascript project. It ran right out of the box, without modifications. Even Expression Blend showed the game and it could be edited in there. A great thing I noticed is that, other than IE10, the jsfxr library works in WinRT applications. Which means this version of the game will have the sounds too.

One of the rules of the contest was to make the code public and publish it on github. So here it is.

If you want to give the game a try, you can do so here (please like-tweet-plus it, thanks).

image

Getting started with Metro, JS and blend

Intro

In this first tutorial in a series about building a Windows 8 application with JavaScript, HMTL and CSS using Expression Blend 5, we’re going to make a start on a very simple game. In this game the player has to guess a random number between 1 and 10. The player wins if the guess is correct, and looses if it’s wrong. In future parts of the series this game will be extended to include various features of WinJS and Windows 8.

File-New

I’ve started with a new “Blank App” from the HTML list. I added a few HTML elements to start from. You can add HTML elements by typing them in the source panel. Or you can drag’n’drop them from Assets pane to Live DOM pane or design area.

File-New

I’ve added the following to the body-tag. Don’t worry about the looks of it, we’ll fix that later.

<h1>Guess a number</h1>
<p>
    <span>Enter a number between 1 and 10:</span>
</p>
<p>
    <input />
    <button>Guess</button>
</p>
<p>
    <span>result:</span>
    <span></span>
</p>

 

The easiest way to access the HTML elements is by assigning IDs. I assigned the following IDs:

  • input ==> numberInput
  • button ==> numberGuess
  • the empty span ==> numberResult

That’s it for the HTML for this moment. Let’s have a look at some code.

Code

Although Expression Blend is an awesome tool, I still prefer to edit my code in Visual Studio. Right click the default.js file and select Edit in Visual Studio to do just that.

image

For now we’re going to ignore most of the code that’s inside default.js. It’s almost all about state management. We’ll talk about that in a later tutorial. I’ve added the following code right below WinJS.strictProcessing(); :

var randomNubmer = Math.round(Math.random() * 10) + 1;

function buttonClickHandler() {
    var value = document.getElementById("numberInput").value;
    var numberResult = document.getElementById("numberResult");
    if(value == randomNumber) {
        numberResult.textContent = "Correct";
    }else {
        numberResult.textContent = "Incorrect";
    }
}

Line 1 of this piece of code generates a random number and makes sure it’s between 1 and 10.

From line 3 and further the click event handler is defined. The first thing this function does is reading the value from the input with the numberInput id. Than, the element for the result is stored. The last thing this function does is comparing the provided value with the random number. If they are equal the text on the result is set to Correct, otherwise it’s set to Incorrect.

The event handler has to be attached to the button. This can be done in the following way:

var numberGuess = document.getElementById("numberGuess");
numberGuess.addEventListener("click", buttonClickHandler, false);

These lines need to be placed after the call to args.setPromise(WinJS.UI.processAll());.

It takes the element with the numberGuess id, and adds an event listener to that. The event handler is told that it should handle the click event and that it should handle the click event with the buttonClickHandler function. The last parameter tells the event listener that it should use bubbling. This means that the event will bubble up the hierarchy of DOM elements and is handled by each element before calling a possible event handler on its parent. Bubbling is what you would want most of the time. The other option, True, means the event listener needs to use capturing. This means the event is handled by the parent first and than the child.

With this piece of code in place it is time to run the application to see if it works. You can hit F5 in Visual Studio or Expression Blend to run the application. Or you can use the interactive mode in Expression Blend.

image

The great thing with this is that you can bring your application is a specific state and continue editing from there.

CSS

If everything’s right you should be able to play the little game now. Time to make it look a little better by adding some CSS styling.

There are a couple of ways to set the CSS properties for the application. You can set them directly on the objects. To give the elements we added in the beginning a bit more space around the border we’re going to change the padding on the body tag. First, select the body element in the Live DOM pane. Now, go to the CSS Properties pane and look in the Layout section.

image

To set the padding for all borders at once make sure the link option, right next to the inputs, is set. Enter 50px in the first input box and note that all boxes are filled with the same value. The CSS properties are stored in the HTML in the style attribute, in this case on the body tag.

To demonstrate a second way of setting styles we’re going to make the title look glowing, bold and blue. We’re going to add a CSS style rule to store the properties. To do that select the title in the designer, right click on it and select Add New Class…

image

The class has to have a name. Enter title in the input box. Because we want the properties to be stored in a CSS style rule make sure the Create style rule checkbox is checked. Hit OK.

image

The title style rule is automatically selected. Go to the CSS properties pane and look at the top section.

image

As you can see the .title rule is selected and that it is stored in the default.css file. The rules below the .title are default rules which are stored in the ui-dark.css file. Because there are different ways in which CSS properties can be set and because they can be overwritten by others all origins of properties are listed here. Each row is loaded on top of the one below. To two options above the line are special. If you select Winning Properties the CSS Properties pane shows a flattened view of all set properties. Very useful to see what values are set. You can even see where a value came from by clicking on the little advanced properties peg next to a set property. In the CSS Cascade section is shown what caused the value to be the way it is.

The computed values option shows a read-only view of all values. Even the ones that are not set.

Right. To make the title look blue select the .title element in the Applied Style Rules list. Go to the Text section. Set the color to light blue, the font-weight to 400 and enter 0 0 5px #4488FF in the text-shadow box. This gives the text a glow look by adding a shadow without X or Y offset.

image

A nice thing to notices is the way Expression Blend tells you were a specific style rule is used. Now that we have the .title rule set, go to the Style Rule pane and select it. At this point the Live DOM pane should have the H1 element highlighted with a dark green background. In the designer the title has a green border. If there would be multiple elements with the selected style rule, all of them would be highlighted in the same way.

image

For the last way of setting the styling I would like to show you we have the add a little code first. In the click event handler, in the check if the number is correct I’ve added two lines. These lines set the CSS class to correct or incorrect based on the result.

if(value == randomNumber) {
        numberResult.textContent = "Correct";
        numberResult.setAttribute("class", "correct");
}else {
    numberResult.textContent = "Incorrect";
    numberResult.setAttribute("class", "incorrect");
}

Switch back to Blend and enter interactive mode. Type a number in the textbox and hit Guess. You’ll probably hit an incorrect number, but for the tutorial it doesn’t matter very much. The .incorrect rule should make the text turn red and the .correct rule should make the text turn green. After having the result shown leave the interactive mode and select the numberResult element. Go to the HTML Attributes pane.

image

A couple of thing are happening here. The first thing to notice are the blue borders around the class box and the textContent property. This blue border means these values are set at runtime. The little lightning bolt next to the class name also indicates this class has been added at runtime.

To convert the dynamically added class to a normal style rule, click on the advanced properties peg, go to Create Style Rule from Element Class and select incorrect.

image

Go to the Style Rules pane, which should contain the .incorrect rule now. Select it and go to the CSS Properties pane. Set the color to red. You can repeat the process until you guess the number, or you can add the .correct rule by clicking the + on the Style Rules pane. Select the rule and set the text color to green.

image

Wrap-up

This first tutorial in the series turned out to be a little longer than I suspected, but I hope you get the basic idea of setting the styles of your apps. If you have any questions just let me know. You can download the code for this tutorial here.

Discovering Expression Blend 5 – ListView

Often you need to display a list of items in a application. The Windows Library for JavaScript contains a ListView control for you to use in your applications. The control can be bound to a data source and templated from HTML.

Getting started

We’ll start off by creating a new project and choosing the Fixed Layout Application project template.

image

As you can see, the template has added a paragraph (p) to the tree which contains some text. You can use that to place the title of your application. The ListView is going to be in the same div as that paragraph.

You can add the ListView by hand, typing the HTML, but I personally prefer dragging and dropping controls onto the designer. Select the parent div of the paragraph.  image

Type ListView in the search box of the Assets panel and drag the result from the list to the design surface.

image

If you switch to Split view in the designer you can see the HTML …

imagewhich should contain the following element.

<div data-win-control="WinJS.UI.ListView"></div>

To be able to write code against the ListView it has to have an Id. You can enter this on the Attributes panel. In this case I named the ListView “posts”.

image

Getting Data

The ListView in this demo application is going to display the Atom feed from this blog. The Windows Library makes this very easy.

Have a look at default.js in your solution. This file contains some code to get you started. When the window is activated the onmainwindowsactivated function is called. This is where the retrieval of the atom feed has to be initiated. First create a new AtomPubClient and a Uri to the feed. Than, pass the url to the retreiveFeedAsync function and have it process the posts or handle the error. At the end of the function you have to call processAll to have all declarative bindings applied on the controls. This is needed to get the templating to work which we’ll create in a second.

I haven’t implemented error handling in the demo, only an empty function. The processPosts function will be implemented after the templated.

WinJS.Application.onmainwindowactivated = function (e) {
    if (e.detail.kind ===
     Windows.ApplicationModel.Activation.ActivationKind.launch) {

        var syn = new Windows.Web.AtomPub.AtomPubClientClient();
        var url =
           new Windows.Foundation.Uri("http://timmykokke.com/atom");
        syn.retrieveFeedAsync(url)
           .then(processPosts, downloadError);

        WinJS.UI.processAll();
    }
}

function downloadError() {
    // handle error
}

Creating a template

The template is created in HTML. It’s basically a div with the data-win-control set like the ListView. The children of that div are used as a template for the ListView.  The template has to be defined before the ListView. To get the bindings to work you’ll have to add the binding.js script to the head section of the HTML file.

The actual binding is done by adding the data-win-control attribute to the divs inside the template. In this case the innerText property of the div is bound to the title property of the data bound object. Note that the children of the template are not shown in the Live DOM panel.

I’ll demonstrate how to set the classes later.

<script src="/winjs/js/binding.js"></script>
...
<div id="itemTemplate" data-win-control="WinJS.Binding.Template" >
    <div class="post">
        <div class="title" data-win-bind="innerText: title"></div>
        <div class="summary" data-win-bind="innerText: summary"></div>
        <div class="date" data-win-bind="innerText: date"></div>
    </div>
</div>
<div id="posts" data-win-control="WinJS.UI.ListView" ></div>

To make the template known to the ListView, select it and go to the Attributes panel. Enter the name of the template in the textbox next to the itemRenderer property. In this case the templated is named itemTemplate.

image

Process feed

The feed returned by the earlier code has to be processed to match the template. It’s nothing more than creating an array (myDataSource), run over the items, adding anonymous objects to the array and setting the data source of the posts to it. If you would like to do some custom formatting on the items, like changing the date to something more readable for example, you can do that here.

function processPosts(feed) {
    var myDataSource = [];

    for (var i = 0, len = feed.items.length; i < len; i++) {
        var item = feed.items[i];
        myDataSource.push({
            title:   item.title.text,
            summary: item.summary.text,
            date:    item.publishedDate
         });
     }

     posts.winControl.dataSource = myDataSource;
}

One of the great things of working with Blend is that you do not have to run your application to see the result and work with it. Try hitting CTRL+R or click on the refresh button in the top right corner of the designer.

imageIf all code is correct you should see the ListView filled with titles and summaries. The Live DOM panel should be filled with elements now. The elements that are generated by code are added to the list to. These elements are marked with a small lightning  bold icon.

image

Now we have to make sure the ListView is showing a ListLayout. With the posts element selected in the Live DOM panel head over to the Attributes panel. Look for the layout property and hit the button next to it.

image

Select ListLayout from the list and hit Ok. Refresh the design if needed.

image

CSS for template

Now we have the ListView filled with data we can set the templates.

Try selecting a title of one of the posts. You might have to click a couple of times, and notice that with every click you drill a little deeper in the element tree.

image

With the element selected head over to the CSS Properties panel. Click on the little triangle and select Create style rule from selected element class… and title.

image

This will create a CSS style rule in default.css for the title class. It is automatically selected. Try changing some of its properties, for example the text color and font-size and see what happens. The height and margins of the programmatically added items is not recalculated on every change, so you may want to hit the refresh button every now and then to update it.

You can repeat the process for the summary and date template items too.

To see what items in the designer are affected by a CSS style rule, select one on the Styles panel. For example the .title rule. Notice how all titles got a green border in the designer and the elements in the Live DOM panel got a dark green background.

image

 

Wrap up

I hope that this tutorial has given you a start on how to use the ListView and how to use templating in Expression Blend. Other controls work in pretty much the same way. Feel free to drop my a line if you have any questions regarding this tutorial or anything else you run into.

For future tutorials on Expression Blend please subscribe to the rss feed or follow me on Twitter (@Sorskoot).

Discovering Expression Blend 5 – Styles

In my previous article I explained the different templates of the Expression Blend 5 preview. Today I would like to walk you through some of the basic of using CSS, or Cascading Style Sheets, in Expression Blend 5. You’ll need CSS to define the look and formatting of your Windows 8 Metro Style application.  Expression Blend 5 helps you create your styles in pretty much the same way you are used to when working on Silverlight or WPF applications in Expression Blend 4 and earlier. But not entirely.

 

Defining a style

For sake of simplicity I’ve created a new solution in Blend using the Blank Application project template. This template comes with three CSS files. Two files are for the default Metro style. One defines a light UI, ui-light.css and one that defines a dark UI, ui-dark.css. Only one of these files is linked in the default.html file, the dark version. I won’t recommend changing these files, but adding you own styles to a new CSS file or adding them to the third CSS file in the project, default.css.

Style sheets linked to the currently opened and active file can be found on the Styles panel.

image

On this panel you can quickly create new style sheets, link existing CSS files or add a style block to you html file.  You can also add new style rules to a style sheet. Because there can be a large amount of style rules available a search box is available to help you find your rules very fast.

We’ll start by adding a rule to the default.css file by clicking on the “ + “ symbol next at the end of the line.

image

The style sheet unfolds and a new rule is added to the list. Type #YellowHeader and hit enter. The “ # “ indicates this rule will be applied to an html element with YellowHeader as its ID.

image

Setting Properties

With the newly added style rule selected have a look at the CSS Properties panel. At the top of panel you’ll find the name of the rule, in this case #YellowHeader. Below that is a search box to find properties. A checkbox for filtering the list is next. Check this to hide all empty properties and show only the ones that are set to certain value. The last part of the CSS Properties panel show a list of all available CSS properties. You can group these per category or show them as a long list ordered by name.

image

As the name of the style rule implies, we’re going to make a header that is yellow. Navigate to the Text category and click on big box next to color. The Color Editor pops out where you can select a color. Pick yellow. Note that the little square at the end of the line becomes white indicating a value has been set, different from the default.

image

Apply Rule

Now we’ve got a style rule defined we’ll need to add it an html element. We’re going to apply this to an H1 tag in the html file. Set the designer to split view to be able to edit the raw html by clicking on the little split button on the upper right corner of the design panel.image Add the following between the start and end of the Body tag:

<H1>A Yellow Header</H1>

The element immediately shows up in the designer and the Live DOM panel too.

image

Select the element in the Live Dom panel or in the designer. And look at the Attributes panel now. This panel contains all attributes for the selected html element. All elements can have an id which uniquely identifies the element. To match this with CSS rule we’ve created earlier name it YellowHeader. Notice that in the list of attributes the id changes too, the little square becomes white and the id also shows up in the Live DOM panel.

image

Because we’ve created the rule before setting the id, the H1 element immediately turns to yellow.

image

One last great feature that was added is to quickly find out which elements are affected by a certain style rule. Select the #YellowHeader rule on the Styles panel and watch the border around the selected element in the designer turn green. Notice the element in the Live DOM getting a dark green background also.image

Inline Styles

There’s more… In this case we’re going to work the other way around. We’ll start with some html elements and add styles for that.

Start by adding the following lines to the body of the html file, right below the H1 tag we added earlier.

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

This creates a list with 3 items.

image

Select the first LI element. On the CSS Properties panel, have a look at the Applied Rules box. Because the styles are cascading and this stack of applied rules can grow bug, it’s sometimes hard to find out what’s the winning rule. This box helps you with that. When winning is selected the properties show what properties are set. Try selecting the bottom one in this case and note all three items are surrounded with a green border again. This is because the selected rule is applied on all LI tags.

Select inline in the Applied Rules box to create an inline style. When this is selected, the changes you make to the properties are placed inside the html tags.

image

Lets change a couple of properties:

  • Set background-color to dark blue
  • Set font-size to 15pt
  • Set font-weight to bold
  • Set line-height to 25pt

The item should look something like this now:

image

CSS Class

Very often CSS uses classes (not .NET classes with code, but CSS classes) to define the looks and formatting for multiple elements at once. Lets convert the previously created inline style to a class and apply it to the other list items.

With the second LI element selected go to the Attributes panel and write redItem in the textbox behind class.

image

Next, switch to the CSS Properties panel. Click on the little tiny triangle in the upper right corner of the Applied Rules and select Create style rule from selected element class… –> redItem.

image

Change the text-color to red, the line-height to 25pt and the background-color to dark red.

image

Select the third LI element now and go to the Attributes panel again. Enter redItem in the box behind class and watch the third item turn red too. Try selecting the .redItem rule on the Styles panel too and notice both elements get a green border.

Wrap up

I hope you get the basic idea of how to work with CSS styles in Expression Blend 5. If you haven’t used CSS before, have a look over at www.w3schools.com. You’ll find lots of information there about CSS and CSS3, along with much more information about other web development/design topic.

For future tutorials on Expression Blend (and I’m sure many will follow) please subscribe to the rss feed or follow me on Twitter (@Sorskoot).

Discovering Expression Blend 5 – Templates

At the BUILD event a lot of great applications were released. The big one of course was the Windows 8 preview. One of the released ISOs contains a couple of preview version of development tools. Expression Blend 5 preview is one of them.

 

This preview of Expression Blend 5 doesn’t let you create Silverlight, WPF or Windows Phone 7 apps. Instead it does what a lot of people where hoping for: Editing/Designing Html5, Css and JavaScript.

Project Templates

The first thing to notice are the new project templates. There’s only one category in the preview: Windows Metro Style. The category contains 5 templates. All templates help you getting started creating Html applications for Windows 8 in Metro style. The Language and Version dropdown lists only contain one element in the preview version. I assume these will grow over time, when Silverlight, WPF and WP7 return.

image

The Blank Application template creates a solutions with only the minimal elements needed. The designer shows a tablet-shape around the surface, like you might be used to when working on Windows Phone 7 applications. The solution contains a couple of Css and .Js files needed for the Metro Style. A manifest file is also added which defines what is needed to run the app on Windows 8.

image

The second project template, Fixed Layout Application, adds some basic layout to the blank application.

image

The Grid Application template is where things get a lot more interesting if you’re just starting writing these kinds of apps. This template contains a couple of .Html files glued together (I might dive into this in a later tutorial) with html attributes. It’s a basic starting point  a grid application and contains some navigating from master to detail views.

image

The Navigation Applications template creates a solutions which looks a lot like the last, but blank. The basic Html elements are in place. This would be a great template to start you applications.

image

The last template, Split Application, create a more complex solution again. It demonstrates how to create a master/detail application showing the master and the details side by side.

image

Item Templates

Besides new project templates, there are new Item Templates too. Adding new files to the solution is something you would to in an Html/JavaScript application too, right? Besides a basic JavaScript File, Style Sheet or Html page, there some templates that will help you develop your Windows 8 applications faster.

image

The Landing Page adds a Html page, a Css file and a JavaScript file to the solution. The page contains a header with a button and a title and a section containing a list. Again, the data is defined in the JavaScript file. The JavaScript code wires thing up to create an interactive page.

image

The Split Page template shows a lot of resemblance with the Split Application project template. The item template adds a Html file to the solution defining the layout of the page. A Css file is added for the styling and a JavaScript file is added which contains the code to make things interactive. The JavaScript code is responsible for creating the sample data too.

image

The Collection Page item template defines a large item and a list of smaller items. Again a Html, Css and JavaScript file are added to the solution. It should be pretty easy to extend the JavaScript code and make the small items navigate to different pages in the solution.

image

The Category Page template adds a page with a horizontal list of images with some details below them. The JavaScript file that is added is pretty much the same as the ones in the previous templates and again, it should not be hard to modify and extend this code to add navigation to the page.

image

The Html Fragment adds an almost empty page. It contains the most basic styles and elements for a Metro style application. The JavaScript file that is added contains only a few lines of code handling the fragmentLoad event.

image

The Details Page contains a large text over multiple columns. The JavaScript file that goes along with this template initializes the page and fills it with sample data.

image

 

That’s it for the templates.

Wrap-up

Although this first preview of Expression Blend 5 only does Html, I think it’s moving in the right direction. Looking at the .Dll files in the root directory of Blend reassures me that the future will bring everything we’re used to back to one of my favorite tools.

Great job Expression Blend team! Keep up the good work.

For future tutorials on Expression Blend (and I’m sure many will follow) please subscribe to the rss feed or follow me on Twitter (@Sorskoot).

HTML5 – An Introduction – Code and Slides

Today I gave a talk about HTML5 at the UNIT4 DevDay. Thanks to UNIT4 for giving me this opportunity and thanks to everyone who attended my session!

Although HTML5 far from completed, it is a lot of fun to work with. Not all tags used in HTML5 are supported by the browsers. You can try the demos on different browsers to see if they work.

  • Download the code here.
  • Download the slides here.

For those at UNIT4 who missed this presentation, I will repeat it at UNIT4 Internet Solutions and at UNIT4 Oost Nederland.  So maybe I’ll see you there. The dates this will happen follow as soon as they are known.

If you have any questions, ideas, suggestions or a shout-out, just let me know by writing a comment below this post, sending me an email or contacting my though twitter @Sorskoot.

197328470

Technorati Tags:

Twitter