Silverlight: web service calls fail when OOB

Situation: your Silverlight 3 application is calling a web service and while it works fine in the browser, it miserably fails in out of the browser mode (OOB).
Don’t panic. It seems that you cannot call a webservice (in App.xaml.cs) before having assigned the application’s root visual. As mentioned, this is not a problem when the application runs inside the browser.

So, for example, this will fail (timeout):

private void Application_Startup(object sender, StartupEventArgs e)
{
    myService.MyMethodCompleted((s, e) => this.RootVisual = new MyPage());
    myService.MyMethodAsync();
}

while this will work fine:

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new MyPage();
    myService.MyMethodAsync();
}

On a side note, remember that RootVisual can only be set once. I haven’t checked what happens with Silverlight 4, but if you are seeing weird behaviours it may be worth it to check this before setting everything on fire.

I hope I saved you long hours sniffing http traffic, checking for crossdomain issues, etc… :-)

Happy coding!

Silverlight/WPF RGB color in c#

Sometimes you have a color in XAML and want to use it in c#. In other words you want to translate something like:

<Border Background=“#AA0FCC1B”>;

to:

Border.Background = …something…

So you start looking for an IValueConverter that create a color from a RGB string, or translate the hex values to decimal, etc… STOP it!
All you need is:

Border.Background = new SolidColorBrush(
                            Color.FromArgb(0xaa, 0x0f, 0xcc, 0x1b));

doh

Ok, it may sound stupid, but you never can tell…