Skip to content
Mar 7 12

Data binding to an ObservableCollection which is updated from another thread in WPF

by mstrid

The problem

The ObservableCollection class in C# is not automatically supporting updating the view/GUI via data binding if it is updated from another thread. This differs from how data binding is handled when binding to an instance implementing the INotifyPropertyChanged interface, where this is handled automatically.

If you attempt this by data binding the ItemSource proprety of a ListBox you get a NotSupportedException with the following exception message:

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.

It looks like this when debugging in Visual Studio:

SNAGHTML3f8009

See the attached source code for the complete sample, sorry for the naïve implementation with a mix of data binding, code behind and implementation in the constructor but I think the basic idea is pretty simple to understand. Smile

 

The solution

Credits where credits are due, this solution is inspired by this blog post.

The best solution I have found so far is to extend the ObservableCollection a bit to make it handle the dispatching to the right thread. It expects that the collection is created from the GUI thread so that it can save a reference to the dispatcher.

The basic pattern in all methods is like this (for the complete source of the class see the attached file):

image

Hope this helps someone!

 

Attached source

ObservableTest.zip

Feb 25 12

Still no font-face support in Windows Phone (7.5) :(

by mstrid

I’m working on a small HTML5 app for handling the daily life todos here at home, like what to buy etcetera, and I thought it would be nice to make it look like a notepad. I found a pretty sweet free font (Jr Hand) in an article over at DesignLovr.com and added to my CSS like this:

image

And when trying it out in Internet Explorer 9 at my Windows 7 desktop it looked ok:

image

But as soon as I test in the Windows Phone Emulator I discovered that the default font is shown and after having googled a bit, reading numerous articles on the topic with various conclusions, I found this article confirming the lack of font-face support in Windows Phone 7.5 (Mango). Back to drawing board selecting one of the default fonts I guess… Sad smile

Feb 9 12

The GTD “two minute rule” and the Pomodoro Technique

by mstrid

I have been struggling with external interruptions when using the Pomodoro Technique for a while but the last days I have had sort of a break-through when I embraced the simple “Two minute rule” from the Getting Things Done process by David Allen.

I work in a team of about 6 software developers and we are surrounded by all sorts of other colleagues, marketing people, clinician, hardware developers etc. This leads to a lot of external interruptions and I have had problems with handling these according to the Pomodoro Technique. Maybe I just took the external interruption handling a bit too seriously but I constantly had problems telling people to wait and that I will come back to them later after my pomodoro has ended.

Now I just make a quick decision on if helping the colleague will take around two minutes or less and if so, do it right away without affecting the pomodoro.

So easy, but so effective. At least for me. :)

Jan 23 12

Pomodoro – my new guide to personal efficiency

by mstrid

A couple of weeks ago I listened to a podcast by Scott Hanselman about different ways to organize your work to get more efficient and I was really captured by the simplicity of the Pomodo technique.

I have now tried this for a week or so at work and I must say that it really helps me to focus and get the right things done. Of course, I have still a long to go to really master the technique but already from day one it made me feel more efficient. There is nothing like checking a thing off your todo list. :)

What I have still to figure out is how to manage meetings, both formal and informal, and other external interferences by colleagues in an efficient way, it is not always possible to postpone things, especially things that only takes a minute or two but they nevertheless disturbs the rhythm of the pomodoros.

Dec 23 11

Connecting Windows 7 on a Mac Mini to a Pioneer plasma

by mstrid

Yesterday I had big troubles connecting my new media center running Windows 7 on a Mac Mini to my Pioneer 42 inch plasma (I think the model is something like PDP 4XX), I kept getting “Input signal not supported” on the plasma.

The setup was the following:

  • Mac Mini Server, originally running Leopard Server but now upgraded to Lion (not server)
  • Windows 7 (bootcamp)
  • Mini Displayport to VGA adapter
  • Pioneer plasma, 42 inch

I tested the media center using an old Viewsonic VP191b monitor, set the resolution to 1024×768 and all seemed fine but as soon as I disconnected the VGA cable from the Viewsonic and attached it to the Pioneer plasma I got and error message ”Input signal not supported”. I rebooted the Mac Mini and switched to Mac Os X Lion and this worked fine on both the Viewsonic and the plasma. Switching back to the bootcamp:ed Windows 7 and the picture on the Pioneer was lost again.

I googled and pulled my hair since noone seemed to have had this problem in this specific setup. Just before giving up I suddenly remembered good old UltraVNC and this saved the day. I had used remote desktop to connect to Windows 7 at the same time as the Pioneer was connected and this worked bu remote desktop kind of uses its own resolution. UltraVNC on the other hand uses the resolution the monitor uses so this made it easier to debug why the Pioneer got an incorrect resolution

The problem was that the Pioneer plasma is somewhat picky when it comes to accepting input signals. Using UltraVNC to connect to the media center (after first having installed the server software using remote desktop) I saw that Windows 7 changed the resolution when attaching the VGA cable to the Pioneer. I had set the resolution to 1024×768 when connected to the Viewsonic but this was, for some reasone, changed to 1280×1024 when the Pioneer was connected. Looking through the table of supported resolutions I quickly could change to 1280×720 and immediately the picture appeared on the plasma!

So in short, do like this:

  1. Connect using remote desktop or using a monitor.
  2. Install UltraVNC Server.
  3. Connect the plasma using the VGA cable.
  4. Connect to the Mac Mini using UltraVNC from another computer.
  5. Change the resolution to one of the supported resolutions.
Dec 3 11

HTML5 or Silverlight? – The million dollar question

by mstrid

Ok, here we go, I guess it is time to make some kind of decision on which way to go.

After having read the post by Scott Hanselman on this topic and since I am thinking about rebuilding an existing website that has some Silverlight content on it I realize that I, for the first time, have to make a decision on whether to use HTML5, Silveright or maybe both.

I googled a bit more and read most of the comments on the post mentioned above and for me it is pretty clear which way to go (at least today):

Fo public websites, I will use HTML5 together with the excellent supporting JavaScript libraries (e.g. jQuery, Modernizr, Knockout, …) as the default choice and add an occasional Silverlight component or two when there simply is no alternative.

Actually it feels a bit sad since I really, really like Silverlight and its development environment and I have invested a huge amount of time learning it to get to where I am today. In addition I have always tried to stay away from JavaScript mostly beacause the development environment has felt so inmature to me. But you can not simply deny the importance of getting your websites onto the tablets and smartphones, and I think that websites that are too Flash or Silverlight intensive will lead to less visits and in the end to lesser sales.

With this said, I still think Silverlight will be an excellent choice for internal web applications where the execution environment is very static. The combination of easy deployment over the web, data handling support and XAML makes Silverlight so powerful.

So, I guess it is time to take a look on the new HTML5- and JavaScript-supporting toolkits for Visual Studio. :)

Nov 15 11

Reflections from DevConnections November 2011

by mstrid

A week has passed since I’m came back from DevConnections in Las Vegas (and a couple of days in SF), and I must say that I’m still impressed! Not that I had low expectations, not at all, but the mere number of developers in one place and all really good sessions to choose between made me feel very inspired.

So back in the dull darkness that is Gothenburg in November it is all about keeping the good spirit from Las Vegas and hopefully try to implement some of the great ideas that we brought home. Here are som highlights:

  • Scott Hanselman’s talk on all useful NuGet packages was awesome and, still suffering from some jetlag, I found his keynote really, really funny. Presentation skills, a sense of humour and a handful or nerdiness really makes a good speeker!
  • Azure seems really easy to use, but it is a bit hard to try to predict the cost in SEK/USD beforehand.
  • Billy Hollis held a session on designing for touch that makes me long for some Windows 8 tablets. :)
  • Juval Löwy’s “The Architect” was a real eye-opener on a business in crisis and what to do about it.
  • Last, but not least, TFS 11 seems really good, especially the built-in task board for us doing Scrum(like) processes.
Sep 7 11

Accessing the Västtrafik API from Windows Phone 7

by mstrid

[NOTE: The Västtrafik API has changed so this post is obsolete.]

I have been working on a Windows Phone 7 app that would make my commuting to work a bit easier (Västtrafik is not the most reliable commuting service ;) ).

Västtrafik publishes a very neat web service API that is free to use if your are registered in their community (http://labs.vasttrafik.se) and here is some code snippets on how to use it:

public class Class1
{
    private const string VASTTRAFIK_ID = "...";

    private const string STOP_ID = "00012711!-1186562910";

    protected Class1()
    {
        this.wcfClient = new VasttrafikNextTripService.NextTripSoapClient();
        this.wcfClient.GetForecast1Completed += wcfClient_GetForecast1Completed;
    }

    public override void UpdateAsync()
    {
        this.wcfClient.GetForecast1Async(VASTTRAFIK_ID, STOP_ID);
    }

    protected override void EndUpdate()
    {
        this.wcfClient.Abort();
    }

    private void wcfClient_GetForecast1Completed(object sender, VasttrafikNextTripService.GetForecast1CompletedEventArgs e)
    {
        if (e.Error == null)
        {
            var result = ParseXml(e.Result);
        }
        else
        {
            this.latestVasttrafikTripInfo = null;
        }
        this.OnTripInfoUpdated();
    }
}
Aug 3 11

Workaround for the sound problems on my Samsung Omnia 7

by mstrid

I really love my Samsung Omnia 7 Windows Phone, the OS is awesome and the screen the best I have seen but  like many others I have experience really bad sound/call performance (e.g. disconnected calls, strange sound quality, etc). I have the Swedish operator Telia and it seems like these problems are operator/network specific. There is an extensive thread on Telia’s forum where you can read much more about this (and the really crappy support from Samsung regarding these problems).

I have been able to create some kind of workaround for the problem which works for me:

DISCLAIMER: This is in no way a general solution since the only thing it does is to prevent the device from switching to GSM. The workaround requires pretty good 3G coverage in all places you go.

  1. Install the Diagnosis app by “dialling” ##634#.
  2. Start the app which now is installed like any other app.
  3. Enter *#2263#.
  4. Select “WCDMA Band Preference” by typing 2.
  5. Select “WCDMA ALL” by typing 6.
  6. Click End and exit the app.

Hope this could help anyone else, at least it made my life much easier.