Rx (Reactive Framework) Can Improve UI Responsiveness

By John at November 24, 2009 14:51
Filed Under: Techy

I’ve been soaking up all the new Rx stuff coming out of Microsoft recently.  Basically it takes the concept of IEnumerable and flips it around into a type IObservable (gotta love duality…).

One of the areas in myOSity I’ve not been entirely satisfied with is the real-time search capability in the File System dialogs.  The feature allows you to type keywords into a text box, and the file list updates below in real-time.  However, this was previously entirely synchronously, such that long searches would hang the UI thread and cause unseemly lag in the responsiveness of the keyboard input.

The general format of this old method was like so:

  1. _app.tbautoSearchByTags.TextChanged += (sender, _) =>
  2.        {
  3.            var tb = (TextBox)sender;
  4.            FileSystemService.Current.VM.SearchCriteria = tb.Text;
  5.            FileSystemService.Current.VM.FilterBySearch();
  6.        };

 

Pretty standard stuff:  I subscribe to the event handler of the textbox’s TextChanged event, and then run my search query, feeding in the the .Text property of the TextBox.   This however, yields the laggy responsiveness that is so undesirable.

What I want to do is to only call my search function when the user has paused their typing for some period of time (.5 seconds, for example).  I could write my own timer logic, but as you will see, Rx handles this much more elegantly and in a much more composable way…

  1. var textObserver = (from text in Observable.FromEvent<TextChangedEventArgs>(_app.tbautoSearchByTags, "TextChanged")
  2.                     select text).Throttle(TimeSpan.FromSeconds(.5));
  3. _searchObserver = textObserver.Subscribe(textChangedEvent =>
  4. {
  5.     var tb = (TextBox)textChangedEvent.Sender;
  6.     FileSystemService.Current.VM.SearchCriteria = tb.Text;
  7.     FileSystemService.Current.VM.FilterBySearch();
  8. });

 

There are two main pieces to the above code.  First I am defining an IObservable (textObserver), which is a lazy Linq query.  You’ll also notice the extension method .Throttle, which prevents the observable from firing any data to the observer unless the given time has passed without any events occurring.  Said another way, the event stream from the TextChanged event must be quiet for .5 seconds before any observers will receive data.  Neat!

The second piece instantiates the observer via the .Subscribe extension.  Here I’m using a lamba to create an anonymous observer.

Now with the Rx magic in place, my UI is not making wasteful calls to the search function, but only when the user is “finished” providing input.  Improvements are still possible.  For example, I could make the search function asynchronous, and implement IObservable<T> on it.  This will help free up the UI thread even further while the search function does it’s work, and only return the result when complete. 

Silverlight Behaviors Example

By John at November 02, 2009 15:57
Filed Under: Techy

I would encourage everyone who works with Silverlight to take a good hard look at Behaviors.  A Behavior is basically some code-behind logic that operates on Silverlight UI elements, bundled into a nice little package that can be in Xaml.  Behaviors can support parameters as well, making them both flexible and extensible.  In Expression Blend 3, several behaviors are provided out-of-the-box.  There is a drag/drop behavior, and a behavior that allows you to control storyboard animations based on events, just to name a couple. 

Below is a Silverlight example of a Rectangle that uses these two behaviors.  When the mouse enters the rectangle, it will alternate colors between red and yellow.  You can also drag the rectangle around.  Check it out…

And here is the Xaml representation of the above Silverlight application:

  1. <UserControl
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  5.     xmlns:im="clr-namespace:Microsoft.Expression.Interactivity.Media;assembly=Microsoft.Expression.Interactions"
  6.     xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"
  7.     x:Class="BehaviorsDemo.MainPage"
  8.     Width="640" Height="480">
  9.     <UserControl.Resources>
  10.         <Storyboard x:Name="BlueToYellow" AutoReverse="True" RepeatBehavior="Forever">
  11.             <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectDemo" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
  12.                 <EasingColorKeyFrame KeyTime="00:00:01" Value="#FFFEFF00"/>
  13.             </ColorAnimationUsingKeyFrames>
  14.         </Storyboard>
  15.     </UserControl.Resources>
  16.     <Canvas x:Name="LayoutRoot" Background="White">
  17.         <Rectangle x:Name="rectDemo" Fill="#FF000BFF" Height="51" Width="58" Canvas.Left="202" Canvas.Top="116">
  18.             <i:Interaction.Triggers>
  19.                 <i:EventTrigger EventName="MouseEnter">
  20.                     <im:ControlStoryboardAction Storyboard="{StaticResource BlueToYellow}"/>
  21.                 </i:EventTrigger>
  22.                 <i:EventTrigger EventName="MouseLeave">
  23.                     <im:ControlStoryboardAction Storyboard="{StaticResource BlueToYellow}" ControlStoryboardOption="Stop"/>
  24.                 </i:EventTrigger>
  25.             </i:Interaction.Triggers>
  26.             <i:Interaction.Behaviors>
  27.                 <il:MouseDragElementBehavior />
  28.             </i:Interaction.Behaviors>
  29.         </Rectangle>
  30.     </Canvas>
  31. </UserControl>

As you can see this is about 30 lines of Xaml that I was able to produce with simple point and click in Expression Blend.  Notice the elements that start with <i:…/>  These are the behaviors that control the storyboard and mouse drag.

The beauty of Behaviors is that they are so easy to use and are highly reusable.  They can also be quite powerful when combined together to create complex user interactions.

Functional Programming Lecture Series

By John at October 30, 2009 13:23
Filed Under: Techy

Dr. Erik Meijer is conducting an excellent video series on functional programming over at Microsoft's Channel 9 website.

Here is the link: http://channel9.msdn.com/tags/C9+Lectures/

Binding To Computed Properties With RIA Services Code Sharing

By John at October 03, 2009 23:33
Filed Under: Techy

I question came up over at silverlight.net that I wrote an example for.  The scenario is that we want to bind our RIA Service data context to a DataGrid control via a DomainDataSource.  We also want to have an extra column which computes a value based on other data that exists in each row.

My approach to this scenario is to utilize the code sharing feature of RIA Services to pass across a property which computes the value on client-side.  As you will see, the DataGrid and DomainDataSource accommodate this approach rather nicely.

We’re going to keep things simple on the server-side for this example and not associate our Domain Service to any entities in a database.  So add a new DomainService called “DemoService” but don’t associate it with any data.

Ok now that we have a Domain Service, lets create an entity object to work with in a class file called “Person.cs”:

  1. using System.ComponentModel.DataAnnotations;
  2. namespace RIAServicesSharedCodeExample.Web
  3. {
  4.     public partial class Person
  5.     {
  6.         [Key]
  7.         public string Name { get; set; }
  8.         [Required]
  9.         public int Age { get; set; }
  10.     }
  11. }

 More...

Mooovin' On Up (To The East Side)

By John at September 24, 2009 08:06
Filed Under: Non-Techy, Off-Topic, Techy

Over the next several days, myOSity will be relocating to a new server and hosting provider.  The current host has been good to us during early development, but it's time for myOSity to find a home where scalability is not going to be an issue down the road.  So over the next week, expect a few random disruptions to both myOSity and this blog.

This is actually really exciting news, because it means that myOSity is getting closer to v1.0!

Unit *cough* Testing…

By John at September 23, 2009 06:50
Filed Under: Techy

The myOSity project is getting big.  So big in fact, that I’m beginning to have trouble troubleshooting issues in a reasonable timeframe.  I’ve decided that it’s best at this point to spend a little time writing some unit tests, rather than move forward on features.   Once I have all the unit tests written, I’ll begin moving forward again in a test-driven mode, which should help me save time in the long-run.

Using Element-To-Element Databinding to access elements inside DataTemplates

By John at September 12, 2009 15:14
Filed Under: Techy

A common situation I run into is the need to access some element within a DataTemplate.  Silverlight isn't able to auto-generate strongly typed references to these elements in code behind.  In other words, adding the "x:Name=myElementName" property to the element doesn't matter from a code-behind standpoint.  However, we can use the x:Name in a different way to access our element at the right moment. Enter: Element-To-Element databinding.  I think many of us know how to bind to property of another element, but it is also possible to bind to the element itself.

The scenario:

More...

GridSplitter Inside a TabItem

By John at September 12, 2009 15:03
Filed Under: Techy

Someone on Silverlight.net was having trouble with this one so I worked up an example.  Here’s the Xaml:

  1. <UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="GridsplitterInTabItem.MainPage"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  6.   <Grid x:Name="LayoutRoot">
  7.         <controls:TabControl>
  8.             <controls:TabItem Header="Tab 1">
  9.                 <Grid>
  10.                     <Grid.ColumnDefinitions>
  11.                         <ColumnDefinition Width="*"></ColumnDefinition>
  12.                         <ColumnDefinition Width="*"></ColumnDefinition>
  13.                     </Grid.ColumnDefinitions>
  14.                     <Grid Background="Red"></Grid>
  15.                     <Grid Background="Blue" Grid.Column="1"></Grid>
  16.                     <controls:GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Left"></controls:GridSplitter>
  17.                 </Grid>
  18.             </controls:TabItem>
  19.             <controls:TabItem Header="Tab 2"></controls:TabItem>
  20.         </controls:TabControl>
  21.     </Grid>
  22. </UserControl>

 

Here’s the working version of it:

Silverlight Developers: myOSity Needs YOU!

By John at August 31, 2009 07:25
Filed Under: Non-Techy, Off-Topic, Techy

Want to be part of something really cool?  I'm reaching the point where I can't do it alone folks, so join the myOSity development team today!  Positions are open in the following areas:

  • UI Design - Have an artistic flair?  You can be the person that develops the look and feel of myOSity. 
  • Core Engine - Get into the guts and help improve/expand the myOSity runtime engine. 
  • myOSity API - Develop the interface and tools that other developers will use to integrate their applications with myOSity.
  • Central Services - More of a server-side type?  You can develop myOSity Web Services and RIA Services, back-end data models, etc.
  • Core Applications - myOSity has several important core applications that need lots of improvement (Applications, Files, Friends, Profile)
  • 3rd Party Applications - Have an app that you would like to publish in myOSity, or something new in mind?

 

Requirements

  • Experience in C# is required and Silverlight experience is a big plus.
  • For designers, proficiency with Expression Design and Expression Blend will be essential for contributing to this project.  Both are quite easy to learn if you have experience with other design tools.
  • Willingness to actively contribute.  I don't require any specific time commitments for this project, but if you do join the team, do so with the commitment in mind that you will be a contributor.

 

What you will receive in return

myOSity is a volunteer gig at the moment (myself included, I've spent over 1.5 years developing this project) so I can't pay you.  However, I can offer the following benefits:

  • Noteriety:  When myOSity becomes world-famous, don't you want to be the one that can say "I was there at the beginning!".   myOSity also becomes a great reference project for your future endeavors/career.
  • Skill-ups: I can tell you from personal experience, that a project of this size and scope will challenge your programming/design skills.  myOSity will help you become a better developer.
  • A job reference: If you contribute materially to this project and need a reference for a job.  I will provide a personal reference for you in writing or over the phone with the hiring manager/recruiter.   Plus having "developer on a next-generation virtual operation system" on your resume/CV isn't a bad thing at all.
  • Future Riches? Maybe:  My intention is to make myOSity commercially viable on it's own, or interesting enough for a larger company to want to buy it for themselves.  There are no guarantees that this will ever happen, but if it does, people on the myOSity team will share in the profits.  And yes I will put that in writing if necessary.

Interested?

Contact me via this blog here.

Silverlight RIA Services

By John at July 20, 2009 14:27
Filed Under: Techy

I'm still trying to build the car, but Microsoft keeps wanting me to tune up the engine!  Might be worth the time and effort though...

Silverlight RIA Services Video

What is myOSity?

myOSity is a On-Demand Operating System that runs in your browser.  You can run applications, work with files, keep in touch and share things with your friends, and more.  The goal of myOSity is to combine the best aspects of operating systems and social networking, while hopefully leaving out all the annoying stuff.

Curious?

To check out myOSity for yourself click here.

Have a game or other Silverlight project that you want to become part of the growing list of applications on myOSity? Contact Me for details.

Join The Team!

Currently looking for other developers interested in being a part of the project.  Contact Me for details or read this post.

About John

I am the founder and lead designer of myOSity.com.  I've been a technologist, in one form or another, for over 20 years.

Protected by Commentor
0 comments approved
4 spam caught
Since December 1, 2008
Powered by Spam Counter