J-Unleashed!

On databases, programming and more

Archive for April, 2008

Pop up a new “window” in Silverlight

Posted by Joe on April 22, 2008

I spent quite awhile over the past few days scouring the web looking for a way to do something in Silverlight that should be a no-brainer. Why is it that when new languages come along, they make you completely relearn everything you’ve known for years and years in order to accomplish the same thing? I digress.

I wanted to simply pop up a window on top of my current window. In the good ol’ days of Visual Basic and C#, it was simply Form.Show() and you’d have your new window pop up with all the controls and content on them. How easy was that?

Ah, but then along comes Silverlight. Mind you, I think Silverlight has some good potential, and some of the demos I’ve seen are pretty neat. However, the simplest tasks seem to take a long time to learn how to do. Try searching for popping up an alert box and you’ll get many references to popping up a Javascript alert box from within Silverlight. Good grief! If I wanted a Javascript alert box with just the OK and Cancel buttons, I would just stick with HTML and move on. I rant . . .

It dawned on me this afternoon that I wasn’t speaking the Silverlight lingo. You see, as I see it now (and I could still be totally wrong), Silverlight is a graphical platform upon which you can draw your content programmatically. A “window” is not a window. It’s a Canvas. Once that lightbulb turned on, it was a little easier to figure out (because I still haven’t found a decent example) how to open up another Canvas. So, I’ll put my code down here for you to see. It’s a real live example of how to open up one Canvas over top of another.

First, we have the XAML file:

<UserControl x:Class="SLFirst.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
<Canvas x:Name="CanvasRoot" Background="Bisque">
<Button x:Name="btnOpen" Content="Open" Width="60" Height="20" Canvas.Top="10" Canvas.Left="10" Click="btnOpen_Click" />
<!-- define second canvas here -->
<Canvas x:Name="Canvas2" Background="Beige" Width="300" Height="200" Canvas.Top="50" Canvas.Left="50" SizeChanged="Canvas2_SizeChanged">
<TextBlock Text="Canvas #2" Canvas.Top="95" Canvas.Left="20" />
<Button x:Name="btnClose" Content="Close" Width="60" Height="20" Canvas.Top="170" Canvas.Left="230" Click="btnClose_Click" />
</Canvas>
</Canvas>
</UserControl>

One MAJOR thing to note is the fact that we currently have to have a SizeChanged event on Canvas2. The reason for this is due to a bug (confirmed by Microsoft) in the current beta that doesn’t display all the objects within the Canvas until you mouseover them. The trick we’re using is to leave Canvas2 visible until the SizeChanged event fires, then make Canvas2 hidden by setting the visibility property to Collapsed. This trick does not work with the Loaded event. For more info on this issue, see http://silverlight.net/forums/t/10906.aspx.

Next, comes the XAML’s .cs file. You’ll note in the XAML file, we have btnOpen, btnClose and Canvas2_SizeChanged events. These will probably be auto-created for you with Visual Studio 2008. If not, you’ll have to created them yourself. Either way, you’ll need to add the code in each event. Here’s the code:

private void btnOpen_Click(object sender, RoutedEventArgs e)
{
    Canvas2.Visibility = Visibility.Visible;
}

private void btnClose_Click(object sender, RoutedEventArgs e)
{
    Canvas2.Visibility = Visibility.Collapsed;
}
private void Canvas2_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Canvas2.Visibility = Visibility.Collapsed;
}

Here’s how it works. The CanvasRoot and Canvas2 canvases load up. The Canvas2 SizeChanged event fires and that runs Canvas2_SizeChanged in the code-behind. This simply makes Canvas2 hidden. When you click on the Open button, that fires btnOpen, which simply makes Canvas2 visible in the btnOpen_Click method. Then, to make Canvas2 disappear again, just click Close, which runs the btnClose_Click event. That method simply makes Canvas2 hidden.

That’s it. I’ll have to play around with this some more, but I can see a lot of good use to this method. Of course, since I know very little about Silverlight, I’m guessing I’m missing something here, but it works for now.

Posted in Silverlight | Tagged: , , | 2 Comments »

Wherefore art thou, o double-click?

Posted by Joe on April 22, 2008

Believe it or not, some of the controls in Silverlight don’t have a double-click event. ListBox is one of them. I am astounded. And, no, ListBoxItems.MouseDoubleClick doesn’t work. So, as usual, I went on my quest to find out how to get double-click to work.

Thanks to a fellow by the name of David Kelley and his blog, HackingSilverLight, I found what I was looking for. Yes, it’s a hack, but a pretty good one, I must say. Take a look at it for yourself here:

http://hackingsilverlight.blogspot.com/2008/02/silverlight-20-double-click-support.html

Posted in Silverlight | Tagged: , , | Leave a Comment »

Wherefore art thou, o toolbox drag ‘n’ drop?

Posted by Joe on April 20, 2008

Yesterday, I installed Silverlight 2 beta 1 for Visual Studio 2008 with the latest version of Silverlight Tools. No issues with the install. I was all ready to start developing something simple, yet cool. Opened VS2008, started up a new Silverlight Application, got the Silverlight environment in front of me, along with the Silverlight Toolbox. First, let’s try adding a label. Oh, wait . . . can’t do that. Labels have been deprecated in WPF in favor of the TextBlock. Ok. Let’s add that. Click on TextBlock, go over to the designer area, attempt to draw a new TextBlock. Nothing. Hmm. Ok, clicked on TextBlock and dragged over to the designer area and dropped. Nothing. Hmm.

Turns out, after searching for others with this problem, that currently, you can’t drag ‘n’ drop into the designer area. You can, however, drag ‘n’ drop into the XAML markup area, and your XAML tags will show up just fine. Ok. That works. Of course, the downside is that you have to position everything very manually for the moment. I guess I can live with that since I am a hand-coder anyway for HTML. Shouldn’t be much different.

I’m glad to say that I was able to set up a quick little app that doesn’t do a whole lot . . . just presents a TextBlock, a TextBox and a Button. Now, I’m off to wire it up to something interesting . . .

Posted in Silverlight | Tagged: , , | Leave a Comment »

Welcome to J-Unleashed!

Posted by Joe on April 19, 2008

I finally decided to break down and get myself a blog. I’m one of those people who has resisted some of the newer technologies coming out on the web. This is almost out of character for me since I have been working with technology since I was 15, and have always been sort of a techno-geek. In the past few years, however, I’ve found myself becoming a bit of a “technosaur” . . . not really latching on to the social networking bandwagon, and steering clear of the Web 2.0 phenomenon.

Well, all that’s about to change. I recently realized that if I don’t get on the bandwagon, my skills will become very outdated, and new job opportunities will pass me by. I’m embarking on a new path. Technologies like Microsoft Silverlight and Adobe Flex will be my playground. I’ll actually understand what Technorati is, and why folks are all a-twitter about Twitter.

Join me on my journey. Welcome to J-Unleashed!

Posted in General | Leave a Comment »