Tag Archive | Silverlight

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)
        if (this.DoubleClick != null)
          this.DoubleClick(this, null);
        _doubleClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 400);
        _doubleClickTimer.Tick += (o, e2) => _doubleClickTimer.Stop();

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

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.

Read More…

Data-Driven Silverlight

I’m continuing the struggle with Silverlight, and I’d like to share my probably worst experience with you. I’d wanted to make a data-driven app in Silverlight recently, but it took a complete day. All I wanted was the binding of a MS SQL table to a Silverlight DataGrid. Because SL doesn’t support System.Data (which made me cry at the first place), I needed a workaround for this one. Then I found the template Silverlight-enabled WCF service, and it was love in the first sight, I thought. To bind a table (one-way binding, of course) in Silverlight is relatively easy. Create a service, set a reference on it, set it as the ItemSource property of the DataGrid. In code:

WebServiceSoapClient client = new WebServiceSoapClient();

client.GetCustomersCompleted += new EventHandler<GetCustomersCompletedEventArgs>(client_CustomersCompleted);

void client_CustomersCompleted(object sender, GetCustomersCompletedEventArgs e)


dataGrid1.ItemsSource = e.Result;


Easy as that.

Read More…

But what’s puzzling you

The one thing that puzzles me nowadays is Silverlight. I use version 3 (because of VS 2010 RC), and heard a lot of good opinions about 4, but I just can’t get hold on it. I have a feeling that it’s not an integral part of the framework (which is true in a sense). After I read Introducing Microsoft Silverlight 3, I thought it’ll be just as easy as put a web page together in Visual Studio. But it’s not. Somehow the entire logic seems to be different behind it. I mean, I just wanted to do simple things in the way I always do them, and have a damn hard time figuring out the Silverlight way of it.

Some examples: play an animation on an event. Okay, it’s fair easy, but I wanted to play it only, if the animation has stopped (in other words, when it’s not playing). But I couldn’t find any property or method to determine whether an animation has finished or not (only a completed event, which was a hard compromise).

I’ve always liked constructors, they provide such a nice way to initialize a variables properties, etc. But when I work with Silverlight, I always have to discover that the developers of it didn’t share my enthusiasm. It’s maybe just my perversion, I found myself a couple of times subclassing ASP.NET server controls just to give them constructors of my taste. But why can’t a DoubleAnimation or a StoryBoard have constructors? I can’t imagine it.

My arch enemy during the development was the static method of StoryBoard.SetTargetPropety(), which takes a Timeline and a PropertyPath type. I can’t even count the errors produced at runtime, because I couldn’t figure out the proper string syntax of PropertyPath. I’ll surely write a post about it.

What made me somewhat frustrated was that I’d liked to dynamically change an image’s source in codebehind. Expected something like this:

im.Source = “/Project;component/Pic/menu1.png”;

But the solution was, which gave me nightmares for days:

string imagesource = “/Project;component/Pic/menu1.png”;

im.Source = ims.ConvertFromString(imagesource) as ImageSource;

Why? Who? How am I supposed to even suspect the existense of a class named ImageSource. What benefit can it provide? As I read the class definition, it helps in some cross-class conversion, but this was definitely not the case here.

I know that I didn’t dig myself enough into this great technology, which would be destined to beat Flash (of which I have no experience) and I truly respect and amaze those, who are able to build such a system, but as a somewhat experienced .NET developer, I excepted that it’ll come into hand in a short time. Anyway, I continue my research in it, books are on their way from Amazon, and I hope I’ll gain a deeper understanding of the whys and hows of its architecture, but till then, I’d be a little disappointed.