Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the biggest gotchas in Silverlight? [closed]

Tags:

silverlight

I've been working in Silverlight recently and I've slowly been discovering that as simple as it can be to develop, there are one or two annoying gotchas that lurk in the shadows.

Considering how difficult some of these have been to track down for me, I thought it might be useful to list them here on SO for others to benefit from. I'm sure the ones I have encountered are nothing compared to the experience of others and I'd like to try and avoid any other nasty or time consuming surprises.

So, what have you got?

Update
I've updated this to cover Silverlight in general. Therefore, please provide a version or list versions to which the gotcha relates when responding. For all those without a version, please assume Silverlight 2.0 as this was the case in the original question.

like image 266
Jeff Yates Avatar asked Jan 26 '09 23:01

Jeff Yates


13 Answers

Message Box

I was working with the System.Windows.MessageBox static class recently (currently, the only way to get something close to application modal - rather than just UI modal - user feedback) and I discovered the irritating focus handling whereby the message box returns focus to the browser rather than the Silverlight control that had focus before the message box was displayed. This is no doubt due to the Silverlight plug-in using the browser to show the message box, but it is irritating none-the-less, and combined with the limited options available for customizing the message box, it was soon apparent that I needed something else to provide my user feedback.

Blocking user interface access when showing a popup...

After searching the Internet, it was clear that I would need to use some kind of user interface modal display based on the Popup primitive. So, I rolled my own modal dialog box class and I discovered another issue in the process.

The user interface beneath the popup remains active which means tabbing around will give you access to the controls you're trying to hide. To mitigate this, I first tried disabling the page by getting the RootVisual (i.e. the page) and setting IsEnabled to false. This worked great, until I used my dialog box during a DataGrid edit operation. The dialog code worked twice and never again. It seemed that disabling and enabling the grid during an edit caused some kind of unrecoverable issue in Silverlight (possibly a memory leak as it eventually crashed).

It turns out that to achieve what I wanted, the easiest way is to set focus to something in my popup dialog and to set the TabNavigation property to KeyboardNavigationMode.Cycle. That way, focus never leaves the dialog and so the underlying UI remains out-of-bounds, but without creating the aforementioned issue.

like image 111
Jeff Yates Avatar answered Oct 03 '22 22:10

Jeff Yates


ComboBox weirdness

One thing that Jeff Yates mentioned in his reply was weirdness with ComboBoxes - I've seen that one personally, and I've also seen another very, very irritating issue with them. If you try to change the items in a ComboBox from the event handler for a button's Click event, you will get a cryptic error message which I can't think of at the moment. My solution to it was to do the same thing as in Jeff's solution - remove the combo box, construct a new one, fill that one with the changed item set, and add that back to the containing panel.

No ElementName binding

Silverlight has nothing analagous to WPF's ElementName binding ability. This post has a good workaround that has worked pretty well for me.

No support for mouse scroll wheel events

There is no native support for capturing mouse scroll wheel events. This post provides a solution that uses browser events for it.

No Unloaded event on UserControls

There is a Loaded event, but no Unloaded - if you want to remove a control from the screen and run some unloading logic, you need to roll your own way to do so. I have an IUnloadable interface that I derive my controls from that need to run unloading logic and invoke a method on it manually when the time is right.


And finally, this post has some good info about what Silverlight is missing as compared to what WPF has.

like image 25
Rob Avatar answered Oct 03 '22 23:10

Rob


Missing a double click event was one for me. I was amazed that is isn't in there and kept looking in the intellisense list :-) Solved it by creating a hack on it, but that's not a solution.

like image 42
Erik Lieben Avatar answered Oct 03 '22 21:10

Erik Lieben


One that got me recently was that there is no LayoutTransform property (although there is a RenderTransform property). Therefore, you cannot apply a transform and have the adjacent elements respect the 'new space requirements' of the control, which will then cause it to overlap with other elements or have blank space.

This was a problem for me because I wanted the scrollable area inside of a scroll viewer to change with the transforms. I was able to get around it, but the process would be much more difficult if you were trying to rotate or do another transform.

like image 20
tsimon Avatar answered Oct 03 '22 22:10

tsimon


Using IE6 and the website hosting the Silverlight App has compression enabled (HTTP 1.1 Headers)

Whenever you make a web service call it just fails even though Fiddler shows the request being sent and the response arriving successfully.

like image 20
Graeme Bradbury Avatar answered Oct 03 '22 22:10

Graeme Bradbury


No Credentials available on web requests

The Credentials property is not available on the WebClient or HttpWebRequest. You need to run the request through a proxy service that will do the credentials.

like image 32
Glenn Slaven Avatar answered Oct 03 '22 22:10

Glenn Slaven


The HttpWebRespone can only have one of two status codes

  • 200 OK
  • 404 Not Found

That means all other errors are mapped to 404, which is quite annoying. I don't know how redirects work (I hope they are being followed automatically...)

http://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(VS.95).aspx

like image 23
chris166 Avatar answered Oct 03 '22 21:10

chris166


The fact that there are so many different releases in a short period of time.

Good - things that are frustrating get fixed quickly. for instance mouse scroll wheel, right click - fixed in Silverlight 4.

Bad - if you're new you have no clue if a documented 'hack' is still necessary or not. I'm starting fresh with Silverlight 4 and finding a tonne of tutorials and help - but never quite knowing if they're up to date or still the best way to do it.

like image 26
Simon_Weaver Avatar answered Oct 03 '22 22:10

Simon_Weaver


A while back I did a project in Silverlight 2.0 and the project was driven using TDD and MVP. My service references were located in a seperate assembly so the view need not know of the model. I had a gotcha with the location of the ServiceReferences.ClientConfig file which needs to be in the view's assembly!

This file is generated if you add a service reference. We were added a WCF Web Service, but I was new to Silverlight and so didn't know that the Silverlight applications are compiled and packaged as a XAP.

If your ServiceReferences.ClientConfig file isn't located inside this XAP you have problems.

So there's my two cents worth. I posted this a while back on the Silverlight forums and it seems I'm not the only one this gotcha applied to.

My Original Post

like image 20
Llyle Avatar answered Oct 03 '22 21:10

Llyle


Oh there are loads. Lets start with 3 Browser gotchas:

Anything but IE: The Silverlight object won't scale to fit the browser window. Workaround: Put this between the tags:

<style type="text/css">
  html, body, form { height: 100% }
</style>

Also be aware that IE will let you have your dev website in localhost and your app access other domains, whereas other browsers won't.

Safari: If you use custom response headers (which are useful for doing efficient paging in Data Services) Safari may change the case of the header names breaking your app.

Visibility issues

There are various issues with changing properties of collapsed visibility components, generally what happens is they don't register the change while they're not visible and you have to do them again when you make them visible. The SL team seems not to have tested very thoroughly what happens when you make components invisible and visible.

If you are getting an unexpected intermittent ArgumentException, this may well be because you have code in a button's click event which makes the button itself (or a parent of it) invisible. Apparently some internal code runs after the click event which is expecting the button to be visible. Get around this by creating a short (100ms for instance) DespatcherTimer in the click event, and setting the button invisible in the Tick event.

DependencyObject / DependencyProperty issues

If you're used to the way these work in WPF they are thoroughly broken in Silverlight. If your data objects are DependencyObjects you will have 2 problems. One is that in SL DependencyProperties don't have change notification built in - if you want them to update bindings you'll need to make your object implement INotifyPropertyChanged. Another is if you bind to a collection of DependencyObjects you will get very wierd effects. You need to use a class a little further down the control hierarchy - I've inherited from FrameworkTemplate instead. So I'd recommend your data objects look like this:

public class CustomDataObject : FrameworkTemplate, INotifyPropertyChanged
{ ... }

Converters

There's no multivalue converter and no Binding.DoNothing. No workarounds for these I know of.

like image 38
James Ellis-Jones Avatar answered Oct 03 '22 21:10

James Ellis-Jones


OpenFileDialog/Isolated Storage

In order for your app to initially use or increase local storage, you have to specify how much space you need, and then Silverlight prompts the user if he/she wants to allow the change. That's all good. But there's a catch: If you open a file dialog, there is no way to obtain more storage after that point. In my scenario, the user selected file(s), I processed them, and then calculated how much space I would need to save the processed file. But because of this issue, I had to guess what the maximum size of that file would be, and ask for storage based on that. If the file was larger, the user would have to do the whole process over again.

Layout system

This really sucked. Until you explicitly set an element's size, you get NaN for all size properties. You can hook into every resize event and get the values there, but in most case you can't just ask for a height or width and get what you want.

No LayoutTransform

Mentioned above by Travis

like image 43
Chris Avatar answered Oct 03 '22 22:10

Chris


Debugging can be tricky, if it's not working it might be because you're attaching to the wrong code type on the process. Try manually attaching to the process and make sure you've got 'silverlight code' selected instead of 'managed code'. From then on it will probably work all the time again.

like image 20
mattmanser Avatar answered Oct 03 '22 23:10

mattmanser


Modal Pop-Ups Not Supported Solution -Create a User Control that expands the entire canvas and set the opacity to .05 as seen on ScottGu's sample Silverlight application http://weblogs.asp.net/scottgu/pages/silverlight-2-end-to-end-tutorial-building-a-digg-search-client.aspx

Datasets not supported Solution - Use LINQ to XML that can read XML into a generic list for databinding purposes

Master Pages not supported Solution - Create a parent XAML page, and have the rest of the application created using User Controls so you can switch between them

Namespace problems occur when using the AJAX toolkit together with the ASP.NET Silverlight tag.
Solution - Use the OBJECT tag instead of the ASP.NET Silverlight Tag.

like image 25
user100985 Avatar answered Oct 03 '22 23:10

user100985