Visiblox » Archive for July 2010

Archive for July 2010

Styles in Silverlight – Inheritance, Precedence and Other Advanced Topics

Thursday, July 29th, 2010

This article is part 2 of 4 in a series attempting to cover everything I think is worth knowing about styles in Silverlight 4. The previous article in the series was Styles in Silverlight: an Introduction that covered what styles are, how they can be defined and some of their limitations.

This article focuses on some more advanced topics that are handy when building more complex applications: re-using the same styles for different types, style inheritance, style precedence and style setter precedence.

Re-Using Styles for Different Types

As discussed in the previous article, all Style elements need to specify a TargetType that specifies the type of class they need to be applied to. If an application uses several different types of visual elements, this can result in having to specify different styles again and again. Let’s say for example our application has an Ellipse and a Rectangle and we want to have both of them have a blue fill. The straightforward way of doing this using Styles is declaring two different styles for each of them and applying those using StaticResources in XAML:

<usercontrol .Resources>
    <style TargetType="Ellipse" x:Key="CustomEllipseStyle">
        <setter Property="Fill" Value="Blue"/>
    </style>
    <style TargetType="Rectangle" x:Key="CustomRectangleStyle">
        <setter Property="Fill" Value="Blue"/>
    </style>
</usercontrol>
<stackpanel Orientation="Vertical">
    <ellipse Width="30" Height="30" Style="{StaticResource CustomEllipseStyle}"/>
    <rectangle Width="30" Height="30" Style="{StaticResource CustomRectangleStyle}" Margin="0,10,0,0"/>
</stackpanel>

However in this particular case it’s possible to combine these styles as Ellipse and Rectangle inherit from the same parent class: Shape. The Fill property that we are setting in the styles is defined in Shape, so we can define a single style with the TargetType set to Shape, then apply this Style to the elements:

<usercontrol .Resources>
    <style TargetType="Shape" x:Key="CustomShapeStyle">
        <setter Property="Fill" Value="Blue"/>
    </style>
</usercontrol>
<stackpanel Orientation="Vertical">
    <ellipse Width="30" Height="30" Style="{StaticResource CustomShapeStyle}"/>
    <rectangle Width="30" Height="30" Style="{StaticResource CustomShapeStyle}" Margin="0,10,0,0"/>
</stackpanel>

So the TargetType of the style does not necessarily have to be the type of the visual element itself. It can be a parent class of the visual element if the setters within this style only refer to properties of this parent class. The XAML code for the second example is shorter, at the same time both examples look the same:

(more…)


Styles in Silverlight: an Introduction

Friday, July 23rd, 2010

This article is part 1 of (planned) 4 in a series attempting to cover everything I think is worth knowing about styles in Silverlight 4.

What are Styles?

Styles in Silverlight are a powerful mechanism that allow controlling the visual representation of elements in a unified way for all user interface elements. The concept is similar to using CSS to control the look and feel of HTML.

Style properties differ by element type. For example on a TextBlock (a text element) one can style the FontSize, FontFamily, Foreground and lots of other properties. On an Ellipse however none of these properties can be set, on the other hand it does provide for example the Fill property, that the TextBlock does not. So creating a blue “Hello, World” text and a red ellipse could be done the following way:

<stackpanel Orientation="Vertical">
     <textblock FontSize="16" FontFamily="Comic Sans MS" Foreground="Blue">Hello, World!</textblock>
     <ellipse Fill="Red" Width="30" Height="30"/>
</stackpanel>

However, to do the same thing, we could simply define Styles. Styles are list of property-value pairs. Creating the same blue text and red ellipse with styles would be done the following way:

<usercontrol .Resources>
    <style x:Key="CustomTextBlockStyle" TargetType="TextBlock">
        <setter Property="FontSize" Value="16"/>
        <setter Property="FontFamily" Value="Comic Sans MS"/>
        <setter Property="Foreground" Value="Blue"/>
    </style>
    <style x:Key="CustomEllipseStyle" TargetType="Ellipse">
        <setter Property="Width" Value="30"/>
        <setter Property="Height" Value="30"/>
        <setter Property="Fill" Value="Red"/>
    </style>
</usercontrol>
<stackpanel Orientation="Vertical">
    <textblock Style="{StaticResource CustomTextBlockStyle}">Hello, World!</textblock>
    <ellipse Style="{StaticResource CustomEllipseStyle}"/>
</stackpanel>

As you can see properties that we’ve specified as attributes on the elements, we’ve listed in the styles and then applied this style to the element. So instead of defining FontSize=”16″ of the TextBlock, we’ve added a Setter that had FontSize set as it’s Property and 16 as it’s Value. (Download the source of this example here: What Are Styles.zip)

Having seen a simple example of using styles, let’s look a little bit deeper in to what they actually are.
(more…)


Silverlight World Cup Mashup Wins Real-Time Push Web App Competition

Monday, July 12th, 2010

A month ago Kwwika, a real-time data streaming service provider annonced a competition to build a mash up that uses their World Cup 2010 Twitter streams. I’ve decided to enter the competition and visualize the stream of tweets in a fun and engaging way. As the competition closed on 11th July, my entry was selected as one of the winners!

The Entry

My entry, Tweet For Your Team shows the tweets of the two teams simultaneously. It measures the live intensity of the tweets (how many are written per minute for each team) and also analyzes their mood by matching keywords and using simple rules to decide whether a tweet carries positive or negative emotions for the team.

The entry also allows visitors to get involved and cheer for or against their team with a single click sending a shout visible to other users off the application. See this post on all the details on the application functionality.

Winners of the competition were announced just before the World Cup 2010 final and I was happy to see my entry being selected one of the two winners. As the judging panel wrote in their summary:
“Gergely created a really engaging application that you could easily sit and watch and interact with during any live World Cup match.”

Why Silverlight?

I’ve used Silverlight to implement the application on top of the live streams. Silverlight turned out to be a great choice for implementing this application for several reasons.

Development time

It took a total of a few days to develop the application, however I was able to finish a working prototype of the application rapidly, in less than 8 hours. From there on I’ve spent most of the time working on the visuals and improving the mood determining algorithm.

Silverlight is indeed great for prototyping quickly and thanks to it’s separated view and code behind concern, it’s quite easy to radically modify the looks of an application down the line.

Performance

During games, the application had quite high load: up to 3500 tweets per minute were coming in per team. This meant up to 10 updates per second on the application interface which Silverlight dealt with surprisingly well after a few tweaks.

Media and Effects

Adding media and visual effects to the application is really easy and quick with Silverlight. I’ve decided late down the road to add some sound effects whenever someone was cheering and vuvuzela background music – all done in a few lines. Also, stunning visual effects can be achieved by using animations and playing with the opacity of elements. I was able to spice the feel of my application up in a few hours of time.

Summary

Silverlight turned out to be an efficient way of creating a visually appealing and performant World Cup mashup in a short period of time. World Cup 2010 is over, so the application isn’t as intense as it was during the games, but I’d still encourage you to take a look at it or watch the video recorded during one of the World Cup games.

Interested in an performant, easy to extend, fully themeable Silverlight / WPF charting library? Give the free version of Visiblox a try!

Advanced Data Visualisation - Powered by Microsoft® Silverlight™ and WPF
© Scott Logic Limited 2011. All rights reserved. Terms of Use | About Us