Visiblox » Gergely Orosz

Gergely Orosz

Common Windows Phone 7 Performance Challenges

Tuesday, May 10th, 2011

This article aims to collect the most important performance challenges I’ve come across when working on various WP7 projects – the most well known being Visiblox Charts for Windows Phone 7 and Cocktail Flow (On a note, I gave a talk on this topic at the London Windows Phone 7 user group – the slides from that event can be viewed from here).

Networking Causing UI Lags

The most common class to use for networking tasks is the WebClient class. It’s the simplest networking class to use, it takes 3 lines to grab a piece of web content:

var client = new WebClient();
client.DownloadStringCompleted += (s, ev) => { responseTextBlock.Text = ev.Result; };
client.DownloadStringAsync(new Uri("http://www.sherdog.com/rss/news.xml"))

Unfortunately there’s a serious issue that’s not mentioned in the documentation of this class – it runs on the UI thread most of the time making the UI unresponsive at times.

The workaround to this issue is to not use WebClient, but use the HttpWebRequest class (which is actually used by WebClient as well). HttpWebRequest has a bit less straightforward API than WebClient and – since it doesn’t run on the UI thread – it’s the caller’s responsibility to marshall back to the UI thread:

var request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.sherdog.com/rss/news.xml"));
request.BeginGetResponse(r =>
{
        var httpRequest = (HttpWebRequest)r.AsyncState;
        var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
 
        using (var reader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var response = reader.ReadToEnd();
 
            Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    responseTextBlock.Text = response;
                }));
        }
}, request);

(more…)


Announcing Visiblox Charts for Windows Phone 7

Monday, February 21st, 2011

We are proud to announce that Visiblox Charts for Windows Phone 7 Beta has been released in version 1.9 of Visiblox Charts and can be downloaded for free here (note: the prerequisite for this release is to download and install the Silverlight Toolkit for Windows Phone). To get started developing on the phone read our tutorial on getting started with Visiblox Charts for WP7.

Visiblox Charts for WP7 is the first charting library that has support for seamingless zooming, panning, and selection – all optimized for the phone. Similar to Visiblox Charts on other platforms it also provides excellent performance, can be easily customized and styled and was built to be easy to extend. For a quick tour on the Windows Phone version, see the following video:

(more…)


Styles in Silverlight: Everything a Developer Needs To Know

Tuesday, February 1st, 2011

This post is a summary on my 4 part series covering everything I think a developer needs to know about Styles in Silverlight in order to work with them in XAML or programatically. The overview of the series is as follows. All of the topics link to the original, more detailed article: feel free to explore the parts that interest you most!

Introduction

Inheritance, Precedence and Other Advanced Topics

  • Re-Using Styles for Different Types: the TargetType of the Style can be a parent type of the target object as long as it only sets properties of the parent type.
  • Style Inheritance: The BasedOn property – inheriting styles can be done using the BasedOn property. Styles support single inheritance and the depth of inheritance is not limited.
  • Implicit Styling – in Silverlight 4 the default styles of elements on a page / control can be specified in a simple way using implicit styles.
  • Style Precedence: a Style’s Setters only get applied if the object’s particular property is not animated, not set locally and not set in the control template belonging to the object.
  • Style Setter Precedence: setters specifying the value of the same property may be declared within the same Style. The last one of them has the highest precedence and will be used.

Further Advanced Topics

  • Style is a Dependency Property. This means that it supports the following features:
    • Data binding: it’s possible to bind styles on to another – even though this is probably a very rare real world scenario.
    • Property changed notifications: it’s not possible to directly subscribe to property changed notifications for the Style property due to the feature missing in Silverlight, however there is a complicated, but working workaround available to detect when the Style is being changed.
    • ClearValue: unsetting the value of a Style is not the same as setting it to null. When wanting to reset or clear the style, ClearValue is the method to be used.
  • Declaring Styles In Code Behind is possible, it results in more complex code though then doing the same thing in XAML.
  • Other Notes About Styles
    • Only Dependency Properties Can Be Styled: styling CLR properties will result in either a compile time or runtime error.
    • Not All Classes Have Styles: only Ones Derived From FrameworkElement have the Style property and thus can be styled. All of the built-in visual controls, the Control and UserControl class are descendants of this class. It’s worth remembering this when creating custom classes that might need to be styled.

Manipulating Styles at Runtime

I hope this summary on styles in Silverlight was helpful.

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

10 Things Developers Will Love About Silverlight 5

Friday, December 3rd, 2010

During the Silverlight Firestarter Scott Guthrie gave a preview of Silverlight 5. Based on the list of new features it seems Silverlight 5 will be at least as significant of an update from SL 4 than SL 4 was from SL3.

In this article I’m looking at the 10 most important things any Silverlight developer – like myself – should be excited about in this upcoming release.

1. Debug Data Binding Expressions by Using Breakpoints in XAML

In Silverlight 4 data binding expressions are great… when you’ve set them up correctly that is. When you want to debug them it’s a real pain to do so. In Silverlight 4 the two ways to debug data binding expressions are:

  • either to look at the debug output window dump and hope that some relevant information is revealed
  • or set up dummy ValueConverters so you could your break when the binding actually occurs – if it occurs!

With the ability to be able to place break points in the XAML on binding expressions, none of this hacking will be needed, saving lots of time and frustration. When breaking on a binding a BindingState object is shown in the Locals windows that returns all information on the binding that should help solve the issue with it.

2. Animations Made Easy with Transitions

Animations are really powerful in Silverlight, the only problem with them is that they’re complicated to write. You need to have StoryBoards, Animations within those and creating and configuring just a simple animation is about 10-15 lines of code both in code behind and XAML.

Silverlight 5 introduces transitions that make defining animations possible in much fewer lines of code. Transitions seem to be wrapper classes around animations. Now instead of having to specify an animation from scratch, you can easily configure the most commonly animated element properties (e.g. opacity, x/y start and end offsets, easing function) on an easy to interpret class interface:

<loadtransition StartXOffset="300" GeneratedDuration="0:0:1.0" StartOpacity="0.2">
   </loadtransition><loadtransition .GeneratedEasingFunction><circleease /></loadtransition>

3. Navigating Up the Visual Tree in Bindings using RelativeSource and Mode=FindAncestor

When using DataTemplates in specifying list items, it would sometimes be great to be able to “reach outside” of the model’s DataContext to another object in the visual tree and use that object as a binding. Say you have ListBox showing off Book items in it’s DataTemplate and you’d like to have a ListBox inside of each of these book items that would show the deals for the book. The problem is, the Book model has no notion of Deals, the Deals object is somewhere else in the visual tree.

In Silverlight 5, this problem can be solved by using a RelativeSource, Mode=FindAncestor expression:
(more…)


Zooming and Panning in Silverlight using Visiblox Charts

Tuesday, November 16th, 2010

Zooming and panning are features that are often needed when using a charting component. This article shows how zooming and panning can be achieved using the free version of Visiblox Silverlight charts with a few lines of code. It then digs a bit deeper to understand how zooming and panning is implemented on Visiblox charts.

The structure of the article is the following:

Setting Up a Basic Chart

Before adding zooming and panning support, let’s create a simple chart with some random data. This is pretty straightforward to do, let’s add a Chart in XAML and in the constructor of the main page auto generate some points:

<usercontrol (...) xmlns:charts="clr-namespace:Visiblox.Charts;assembly=Visiblox.Charts">
    <grid x:Name="LayoutRoot" Background="White">
        <charts:chart x:Name="MainChart"/>
    </grid>
</usercontrol>
// Add 100 points to the chart
var rnd = new Random();
var dataSerires = new DataSeries<datetime , double>(){ Title = "Series 1" };
var startDate = Convert.ToDateTime("2010. 08. 01.");
for (int i = 0; i < 100; i++)
{
    dataSerires.Add(new DataPoint<DateTime, double>(startDate.AddDays(i), rnd.Next(-20, 20)));
}
// Add a line series with the created data series that shows points
MainChart.Series.Add(new LineSeries() { DataSeries = dataSerires, ShowPoints = true, ToolTipEnabled=true});
</datetime>

The result is the following simple chart with a line series:

Adding Zooming and Panning

The Behaviour Model

Both zooming and panning are quite similar in both of them have to respond to mouse events. For such “interactive” behaviours, Visiblox has defined the Behaviour model which is an easy extension point for creating interactive components. Both zoom and pan are such behaviours (IBehaviours). To use them, one simply has to assign either of them as the Behaviour property of the chart. So let’s add that single line to enable them:
(more…)


Styles In Silverlight: Manipulating At Runtime

Thursday, November 4th, 2010

This article is part 4 of 4 in a series covering everything that I think is worth knowing about styles in Silverlight. The previous parts of the series are Styles in Silverlight: an Introduction and Styles in Silverlight – Inheritance, Precedence and Other Advanced Topics and Styles in Silverlight – Further Advanced Topics.

This part of the series explores manipulating, merging and modifying Styles at runtime. Manipulation of Styles runtime is quite an uncommon scenario and is not really documented, this article tries to cover all important aspects of it.

Modifying Styles Runtime: the IsSealed Property

Modifying the Setters of a Style can only be done if the IsSealed property is set to false. This property is false when the Style is created and is set to true once the Style is applied to any element in the visual tree. Thus it’s safe to say that Styles are a immutable in a special way: they can only be modified until applied to any element in a visual tree.

Trying to modify a Style that is sealed (IsSealed property set to true) can result in multiple errors:

  • If trying to modify the value of a Setter in the Style, an
    UnauthorizedAccessException to be thrown with the error message Attempted to perform an unauthorized operation.
  • If trying to add another Setter to the Style, an Exception will be thrown with the (not so informative) error message Error HRESULT E_FAIL has been returned from a call to a COM component.
  • When removing a Setter from the Style’s Setters, no exception is thrown, but the operation will have no result.
  • I’ve created a simple example to demonstrate how modifying the assigned Style object will not work and throw exceptions or not do anything:

    The code behind for the event handlers on the buttons are as follows:
    (more…)


    Styles In Silverlight: Further Advanced Topics

    Thursday, August 19th, 2010

    This article is part 3 of 4 in a series covering everything that I think is worth knowing about styles in Silverlight. The previous parts of the series are Styles in Silverlight: an Introduction and Styles in Silverlight – Inheritance, Precedence and Other Advanced Topics.

    This part of the series explores some further advanced topics: data binding, change notification, declaring styles in code behind and some other useful notes on styles.

    Style is a Dependency Property

    The Style property is a dependency property. This means that they give you some extra features that CLR properties would not. The features that can be used in case of the Style dependency property are the following:

    • Data binding
    • Property changed notifications
    • ClearValue

    Note that dependency properties have two other important features that can’t be used with the Style dependency property: animation and styles:

    • The Style property can’t be animated as it’s value type isn’t supported by any Timeline derived animation type.
    • Styles can’t be applied to the Style dependency property… because this is the property that styles need to be applied through (all dependency properties can be styled except for Style).

    Data binding

    As Style is a dependency property it can be data binded – even though there seem to be very few real world examples when this functionality would actually be needed.

    The following example demonstrates how this feature works. Two Ellipses are used: LeftEllipse and RightEllipse. RightEllipse binds its style to LeftEllipse so whenever the style of LeftEllipse changes, so does the style of RightEllipse. When clicking on the button it changes the style of LeftEllipse and thus through the binding the style of RightEllipse is changed as well:

    The code (XAML):
    (more…)


    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