Silverlight

I’m doing a lot of Silverlight work and one issue I’ve encountered is how to set the cursor to an application wide busy wait icon, and disable all the controls in the application, while a web service call is made. The first technique I tried was to wrap all the application content in a ControlControl and set the latter’s IsEnabled property to false while the service call is made, also setting the cursor to Wait on the main UserControl. This doesn’t look good though because the application is greyed out while it is disabled, which seems too visually aggressive.

A better technique is to place a transparent rectangle over the application content during the service call. For example, in the following XAML the Rectangle element is defined with its Cursor set to “Wait” and its Visibility set to “Collapsed” so that when the application runs the Rectangle will not affect its appearance:

<UserControl x:Class="TestBusyCursor1.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    HorizontalAlignment="Center" VerticalAlignment="Center">  <Grid x:Name="LayoutRoot" Background="LightGray" >    <Border BorderBrush="Black" Cursor="Arrow" >      <StackPanel Margin="50">        <TextBlock Margin="10" Width="300" Text="Busy Cursor 1"/>        <TextBox Margin="10" Width="300"/>        <Button Content="Do Something" Click="Button_Click"           Margin="10" HorizontalAlignment="Right"/>      </StackPanel>    </Border>    <Rectangle x:Name="WaitCursor" Fill="Transparent"      Visibility="Collapsed" HorizontalAlignment="Stretch"       VerticalAlignment="Stretch" Cursor="Wait"/>  </Grid></UserControl>

In the code-behind the click handler sets the Visibility of the Rectangle to Visible and so because the Rectangle is defined after the other content in the Grid control it effectively overlays the other content and prevents the user from clicking on any controls underneath the Rectangle. A worker thread sleeps for 5 seconds to simulate the long-running service call, and then when this completes the visibility of the Rectangle is set to Collapsed again so the Rectangle is hidden:

using System.ComponentModel;using System.Linq;using System.Threading;using System.Windows;using System.Windows.Controls;namespace TestBusyCursor1{  public partial class MainPage : UserControl  {    public MainPage()    {      InitializeComponent();    }    private void Button_Click(object sender, RoutedEventArgs args)    {      WaitCursor.Visibility = Visibility.Visible;      var worker = new BackgroundWorker();      worker.DoWork += (s, e) =>      {        // simulate making a slow web service call        Thread.Sleep(10000);      };      worker.RunWorkerCompleted += (s, e) =>      {        Dispatcher.BeginInvoke(() =>        {          WaitCursor.Visibility = Visibility.Collapsed;         });      };      worker.RunWorkerAsync();    }  }}

Click on the button here to see the Wait cursor and how the button and textbox cannot be clicked during the simulation of the service call:

Get Microsoft Silverlight

One problem with this is that it is still possible to tab into the text box, and enter text there, while clicking on the content is prevented by the Rectangle. This can be worked around by finding all the child controls which have their IsTabStop property set to true and setting it to false for the duration of the service call:

    private void Button_Click(object sender, RoutedEventArgs args)    {      WaitCursor.Visibility = Visibility.Visible;      var tabStops = LayoutRoot.GetVisuals().OfType<Control>()        .Where(c => c.IsTabStop).ToArray();      foreach (var tabStop in tabStops)        tabStop.IsTabStop = false;      var worker = new BackgroundWorker();      worker.DoWork += (s, e) =>      {        // simulate making a slow web service call        Thread.Sleep(10000);      };      worker.RunWorkerCompleted += (s, e) =>      {        Dispatcher.BeginInvoke(() =>        {          foreach (var control in tabStops)            control.IsTabStop = true;          WaitCursor.Visibility = Visibility.Collapsed;        });      };      worker.RunWorkerAsync();    }

So in this version it is not possible to tab into the text box during the service call:

Get Microsoft Silverlight

There is one problem remaining: the visual state of the cursor is not changed until the mouse is moved. This can be confusing if you click on the button and move the mouse for a moment so that the Wait cursor is displayed, but then don’t move the mouse after the service call. From the user’s point of view it looks like the service call is continuing indefinitely. For this reason it may look better to instead use some sort of spinning icon, web browser style, to indicate that the application is busy.

More: continued here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Technorati

Related News


  • Google Open About Kill Switch in Android Phones (NewsFactor)


  • NewsFactor - Although T-Mobile's G1 smartphone with Google's Android mobile operating system won't be formally launched until Oct. 22, observers are busy peering under the hood and reading the fine print. One feature is sure to cause some comment: A remote kill switch that will let Google wipe out any application that violates the developer distribution agreement for Android apps.


  • Creating Order from Chaos with Evernote


  • When he parks his car, author Timothy Ferriss snaps a photo of the nearest cross streets with his camera phone. In business meetings, he'll often take pictures of sketches and notes made on a whiteboard. When he's out for dinner, he'll whip out the phone again to capture an image of the label on the wine he's drinking. He never knows when he'll want to recall the data later.Ferriss, a productivity expert, blogger, and author of the best-selling book The 4-Hour Workweek, then ships those photos to what he calls his "augmented brain," which exists not in his head, but on the Web.He is one of a growing number of people using a Web-based service and software application running on smartphones and PCs called Evernote that is quickly becoming a receptacle for much of the ephemera that otherwise gets cluttered and sometimes lost in a person's busy life.At first, Ferriss resisted the suggestion from readers of his blog that he try the application. "I have this philosophical stance where I tend to avoid accumulating new gadgets and software because usually they create more work than they are meant to prevent," Ferriss says. But when a few reader suggestions turned into dozens, he decided to try it. "At first it wasn't clear what the appeal was. But the more I used it, it became really clear why they liked it."Word Recognition in PhotosFounded by Stepan Pachikov, who co-founded handwriting recognition software company Parascript and is a former vice-president of Silicon Graphics, Evernote is designed for people struggling to become more organized. A February survey by the National Association of Professional Organizers, a trade group, found that 96 percent of some 400 adults said they could save time every day if they were better organized. "No one remembers everything as well as...


  • Answer to “NUnit isn’t running VS10 code”


  • I just posted my first answer to stackoverflow. Brian Ball was getting the following error when trying to load a dll built using VS 2010 beta into the NUnit GUI:This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. You may be attempting to load an assembly build with a later version of the CLR than the version under which NUnit is currently running.My answer was:I've downloaded the NUnit 2.5 source and opened the VS2008 solution in the VS2010 beta. Once the conversion finished I opened all the projects and changed the target framework setting for all the projects to ".NET Framework 4.0". I then built the solution without any errors. I can now use the NUnit GUI app to run tests built for .NET 4.0. I've not done exhaustive testing of this build so there may be problems, but for my purposes it works fine.UPDATEIt is not necessary to rebuild NUnit. I discovered that if you add the following to the relevant NUnit application config file you can run a test dll built for .NET 4.0.Under <configuration> add:<startup> <requiredRuntime version="v4.0.20506" /></startup>and under <runtime> add:<loadFromRemoteSources enabled="true" />Just adding the requireRuntime element is insufficient and results in an security related error message mentioning the loadFromRemoteSources switch. I found something about the switch on this social.msdn post, where David DeWinter wrote:Caveat: I'm not on the security team but will attempt to answer this nonetheless...What's happening here is that the build tasks for Silverlight are attempting to load an assembly that, in previous versions of the CLR, would classify it as a partial trust assembly based on its evidence (e.g. its zone) according to CAS policy.In CLR 4.0, CAS policy is totally deprecated and is not even enabled by default. Under the circumstances, though, it appears the CLR throws an Exception when what would be a partial trust load in CLR 2.0 is a full trust load in CLR 4.0.The loadFromRemoteSources switch the Exception message refers to is in the runtime element under configuration and looks like this:<runtime>    <loadFromRemoteSources enabled="true|false" /></runtime>This will not enable legacy CAS policy but will allow you (or, in this case, the build system) to load remote assemblies with the same permissions as the host AppDomain. In this case it seems as though you could modify the configuration for the build system (which I assume in this case would be Visual Studio: %ProgramFiles%Microsoft Visual Studio 10.0Common7IDEdevenv.exe.config) to enable this switch.If you don't want to modify that configuratino then you can set the environment variable COMPLUS_EnableLegacyCASPolicy to 1, which will enable CAS Policy that was present in CLR 2.0 and also allow Silverlight to load this task.Hope that helps.David


  • [$]


  • Maintaining a complex web application that uses a lot of Javascript forclient-side, "AJAX"-style interactivity is rather difficult. Theclumsiness of the Javascript language itself, as well as the various tricksneeded to make an application work consistently across multiple browsers,all of which must be wrapped up inside HTML, makes for a jumble of issuesfor the application developer. Pyjamas ismeant to ease that development, by allowing client-side applications to bewritten in Python, then translating that code to Javascript for use by thebrowser.


  • Linux now an equal Flash player (Linux-Watch)


  • Linux-Watch reports on Adobe's release of the proprietary Flash Player 10 for Linux."Welcome to the future. Linux is now a first-class desktop operating system citizen. Adobe today released version 10 of its Adobe Flash Player, available now in a variety of convenient packaging formats for Linux, as well as other popular desktop operating systems.Once upon a time, desktop Linux was a second-class citizen, where Flash was concerned. As recently as 2007, Linux users waited six months for Flash 9 to arrive.Now, while Microsoft appears bent on leaving Linux users behind on Silverlight technology, its Flash alternative, Adobe has made Linux an equal player."


    Leave a Reply

    You must be logged in to post a comment.