Silverlight 2 Visibility Bug With The Canvas

So I encountered a bug today. A real-life, this-hasn’t-been-fixed-yet bug in Silverlight 2 Beta 2. And I gotta think it’s pretty common. After all of my posting on layout options, I think it’s my responsibility to mention this one. It is also documented on the Silverlight forums, but I want to make sure it’s shared here as well…

Much of what I am doing for my pet Silverlight 2 project requires me to show/hide different canvases, depending on which “page” you are on within my site. I’ve seen some Silverlight sites that are actually just different SL apps, and links actually take you to a seperate .aspx page for each one. I wanted to keep the user experience without any postbacks. All AJAX. No wavering from my page. Because of this, each navigation button just hides everything and shows the section it represents.

So each “page” is represented as a canvas. And for the simple example below, it works perfectly.

However, when your canvas contains elements that derive from Control, you don’t get the behavior you expect. Here’s my canvas with real stuff in it:

What I am finding is that my canvas just doesn’t show up. In my code, I know my syntax is correct, because it works for all of the simple canvases like the first one I showed. The moment I add a Button, TextBox, Radio Button, User Controls, etc. the Canvas no longer will change its Visibility property from Collapsed to Visible. This, it would seem, is a major problem. However, there’s a pretty simple workaround for this.

Click here to see the example running.

By making my base element for this function something other than a Canvas, it works perfectly. In my example, I have used a StackPanel, and just put my Canvas right inside it as the only element. In this example, all of the contents of my “page” will display as expected now.

Sure, it’s a little work-around-y, but it gets the job done until this bug is corrected.

Has anyone else encountered this? What was your solution?

3 thoughts on “Silverlight 2 Visibility Bug With The Canvas

  1. For my presentation app I had each slide (similar to your pages) created as user controls. They were stored in a list and just swapped when the user navigates between slides. You can check it out at http://www.codeplex.com/PresentLightAlso, you could try setting opacity to 0 instead of visibility to collapsed.

  2. Hi Jeff,I’m not using the canvas as my “pages”. I like the approach that Sean took here: http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx.Basically, each of your pages is a user control (like the default Page.xaml that you get when you start a new project). The idea is that you swap out the RootVisual of your application to switch between pages. That way you don’t have everything in one big Page.xaml, and you can also easily animate between the pages. See the example on his blog – source code is included. It’s pretty cool.Kevin

  3. We are getting into semantics again. If we use words, there is a very grave danger they will be misinterpreted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s