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 4 Paves The Way For myOSity’s Future

By John at November 21, 2009 10:54
Filed Under: Non-Techy

If you are a fan of myOSity, you probably know that it runs on top of Microsoft’s excellent Silverlight platform.  We’ve been with Silverlight since version 2.0 and remain committed to it for many reasons which I won’t get into here (plenty of blogs out there with “Silverlight vs. Flash” posts…).

Silverlight 4 (beta 1) brings a host of new enhancements that will, in turn, allows us to add some really cool features to myOSity in the near future.  Check out this list:

  • myOSity on your desktop!  Technically we could give this to you now with SL3, but there isn’t really a compelling reason to do so.  With SL4, we’ll be able to add a whole host of new capabilities that we can’t offer in the browser…
  • Printing.  SL4 now supports the ability for printing, an oft requested feature from the Silverlight developer community.  Thanks to Microsoft for listening!
  • Html support.  We’ll be able to give you a mini web browser/RSS experience in myOSity without hacks and workarounds.
  • Local file system access.  You’ll be able to drag and drop your local files right into myOSity, and access them from any other computer if you upload them to your myOSity file system.
  • Cross-domain support.  This is a technical term that means we’ll be able to write better mash-up applications, like Twitter clients, Facebook integration, etc.
  • Webcam/microphone support.  Voice & Video-chat anyone?  Don’t expect this feature right away, but eventually we’ll be able to let you chat with your friends with voice and video through myOSity.

 

And much more!  Too many to list really, but you get the idea… good things are coming!  I should also mention that, at this time, Microsoft hasn’t licensed SL4 for public use yet, but rest assured, we’ll be moving quickly to take advantage of the new version as soon as it becomes publicly available.

If you want all the nitty gritty details, you can click here to check out the official announcement regarding SL4 Beta.

Xamla gets the Silverlight.net bump

By John at November 09, 2009 09:55
Filed Under: Non-Techy

Xamla has recently been accepted into the silverlight.net Community Gallery.  You can check it out here: http://www.silverlight.net/community/samples/silverlight-samples/xamla/

myOSity Goes International

By John at November 03, 2009 15:29
Filed Under: Non-Techy

Henry (from the myOSity project team) has recently introduced “localization” support for myOSity, which basically means support for displaying multiple languages.  In fact, he has already started creating the translation file for Chinese.

Below is a screenshot of the myOSity welcome dialog with Chinese localization.

myosity_welcome_chinese

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/

New Application: Garbo Chess

By John at October 13, 2009 10:13
Filed Under: Non-Techy

I've adapted the Microsoft Silverlight demo of chess, the original verison of which can be found over at silverlight.net in the community gallery, to work as a myOSity application.  It ain't pretty (yet) but it works.  Check it out and make a post in the discussion thread of Garbo Chess (see this post for details the new Discussion System) to request features and provide feedback.

Video: Introduction to myOSity Series, Part 3

By John at October 10, 2009 23:06
Filed Under: Non-Techy, Videos

In this third installment, I demonstrate how you can use the myOSity Discussion Framework to have your say regarding just about anything (people, applications, files, and more).

Link to video (WMV format)

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...

Google announces limited launch of “Wave”. myOSity quickly renamed to “Tsunami”

By John at September 29, 2009 18:01
Filed Under: Off-Topic

Kidding about the Tsunami thing, but for a moment there I was tempted.

Wave looks cool, but as I evaluate it, I realize quickly that myOSity will be that and much more, so perhaps “tsunami” isn’t such a bad nickname after all.  Nah, I don’t like tsunamis, they hurt people… :(

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