Double-click in Silverlight

It’s a bit nonsense for me, but in Silverlight, there’s no double click event. A little annoying issue, but fortunately, we can craft our own double click with the help of the MouseLeftButtonUp event and a timer (I’ll show the bare minimum here, let’s call it the poor man’s double click).

First you need a timer. To avoid some nasty threading issues, we use the DispatcherTimer – it runs on the same thread as the Dispatcher. I created it as a private field of my UserControl, so I assume you did this too.

I also created an event for the double click, but it’s fairly convenient. The main interest is the MouseLeftButtonUp event, which looks like this:

private void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
      if (_doubleClickTimer.IsEnabled)
      {
        _doubleClickTimer.Stop();
        if (this.DoubleClick != null)
        {
          this.DoubleClick(this, null);
        }
      }
      else
      {
        _doubleClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 400);
        _doubleClickTimer.Tick += (o, e2) => _doubleClickTimer.Stop();
        _doubleClickTimer.Start();
      }
    }

* This source code was highlighted with Source Code Highlighter.

The source code is pretty simple. First we check if our DispatcherTimer has an enabled state. If it does, it means that it’s already started. If it’s started, then it means that the user must have clicked once on the control. That’s because if the DispatcherTimer isn’t started, then the else branch will run, which does nothing more than start a timer with a reasonable 400 milliseconds double-click experience, sets that it should run only once, then starts it.

So if our timer was started that means we are no longer than 400 milliseconds after the first click, so we have a double click! And we fire the double click event. That’s all, use it, extend it as you wish.

Advertisements

Auto update an OOB Silverlight application

So you created an application, have a shortcut to it on your desktop, but whenever you change it, you have to delete the link and create a new one?

This problem can be solved easily. The following code snippet does it for you:

    1. if (Current.IsRunningOutOfBrowser)
    2.       {
    3.         Current.CheckAndDownloadUpdateCompleted += (s, e2) =>
    4.         {
    5.           if (e2.UpdateAvailable)
    6.           {
    7.             MessageBox.Show(“A new version was found, please restart the Application!”);
    8.             if (e2.Error != null)
    9.             {
    10.               MessageBox.Show(“There was an error updating the application!”);
    11.             }
    12.           }
    13.         };
    14.         Current.CheckAndDownloadUpdateAsync();
    15.       }

 

* This source code was highlighted with Source Code Highlighter.

However, there are some quirks. You shouldn’t check the “Require elevated trust when running outside the browser”, because if you do so, auto update fails to start.

And another one: if you place the code above in the app constructor, it won’t work. The earliest stage I manage to get it work was the Application_Startup method.

Full screen Silverlight

So you’d like your app to run full screen, don’t you? Are you ready to get your brain blown off? Then let’s get started with it.

First rule: you can only enter full screen as a result of a user initiated action. This is a security feature, and I don’t know the reason of it, but smarter guys will tell you if you ask them. So no full screen enters in Window_Loaded event handlers, but you can add a MouseLeftButtonUp handler to the same window, and add the following code to the event handler:

Application.Current.Host.Content.IsFullScreen = true;

Security bypassed, the user initiated the action (although totally unaware of it). You might think that you’re good to go from now on, your application running full screen, everybody is happy. Sadly, there’s a little quirk here: you won’t get full keyboard support running full screen.

I was a little unspecific about what is full keyboard support. Well, you can rest assured that you can’t type in anything, but you can move around with arrows. Nice, isn’t it? Before you ask, no there’s no way to code this around.

From version 4, we have a solution to your problem. Setting the Application.Current.MainWindow.WindowState property to Maximized, you can have a maximized window on startup. You don’t even need the user to initiate an action for you, it just works. The code for the lazy ones:

Application.Current.MainWindow.WindowState = WindowState.Maximized;

It’s not full screen, just a plain old maximized window, but it did the trick for me.

The state of the Silverlight platform

I remember starting this blog one and a half year ago with a furious post on the immaturity of Silverlight (back then I think the version was 2.0). Now I’m working on the biggest project so far in my career, and guess what the platform is? Yes, of course, Silverlight.

So we’re arriving to version 5.0 in a matter of days, and we’re also having (or at least I’m having) nightmares about the future. After watching the BUILD conference, scanning through the news, blogs, forums, whatever I had the feeling that Silverlight won’t last that long. (Of course, Silverlight is a first-class citizen of the new Windows 8 platform, but we didn’t even mention it in the keynote). After the first suspicious thoughts came the campaign of Silverlight’s importance, which further strengthened my feelings that something is wrong here. But enough of this.

Continue reading