J-Unleashed!

On databases, programming and more

Posts Tagged ‘Silverlight’

Thank You, Michael Sync!

Posted by Joe on May 3, 2008

I tried out the step-by-step example by Michael Sync for getting data into a datagrid in Silverlight via webservices. I skipped some steps as I already had a database set up, and I adapted the code to my specific needs. I’m very, very glad to say, though, with very little difficulties, it worked. I did have to surmise a few things, so I’ll jot them down here.

  • You’ll need to create the ListingControl.xaml file. The easiest way would be to rename the default Page.xaml that’s created in the Silverlight project.
  • In the ListingControl.xaml file, rename the default <Grid> from “LayoutRoot” to “ListingControl”
  • Add a “Loaded” event to the <Grid> and choose the default name (ListingControl_Loaded)
  • Add a DataGrid to the ListingControl.xaml file.

Here’s the code for the XAML file:

<UserControl xmlns:my=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”  x:Class=”SLTwo.Page”
xmlns=”http://schemas.microsoft.com/client/2007
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
Width=”400″ Height=”300″>
<Grid x:Name=”ListingControl” Background=”White” Loaded=”ListingControl_Loaded”>
<my:DataGrid x:Name=”CustomersDataGrid” Grid.Row=”1″ Margin=”5″ AutoGenerateColumns=”True” />
</Grid>
</UserControl>

  • Note that you may have a different solution name than the example (S2WebSrv). Mine was named SLTwo, so I had to change that in the <UserControl> tag.
  • Be careful with your XML. My data had ampersands in various places, so I had to change the line of code in the RetrieveProduct method of the web service that adds the product name to the XML so it replaces “&” with “&amp;”. The XDocument.Parse method choked on the XML it was receiving, but didn’t say why. Ugh.

Phew! What a long road just to get data flowing into a Silverlight app. Glad to say, with Michael Sync’s help, that obstacle has been overcome. Now, on to better things. Maybe I’ll try hooking up NHibernate to the ASP.NET project and play around with some more interactive content.

‘Til next time!

Posted in Silverlight | Tagged: , , | 1 Comment »

Rant: Web Service Woes

Posted by Joe on May 2, 2008

I won’t rant too much here. Just updating on my latest endeavors with Silverlight. I was trying to wire up my test app to a MySQL database, but discovered you can’t easily do that because you can’t use a .dll (e.g. the MySql.Data.dll) in a Silverlight Application. That’s kind of a bummer. It turns out the Silverlight way of connecting to any data source is through a web service.

I have no real issues with this, except I wonder how performance will be on larger datasets. I’ll have to run some tests once I get it working . . . IF I get it working. For the life of me, I can’t get web services to work. I’ve seen a few dozen examples, but when trying to implement them myself, I run into blocks all the way. Hooking up to a standard .NET web service (.asmx) is painful, but doable. Looks like Silverlight prefers the WCF-style web service and Linq.

I coded up a WCF web service, then used Linq in Silverlight to read the data returned from the web service. Note, however, that I made an assumption that I would create one true .NET solution (non-Silverlight) that handled the data access and web service portion, and consume that web service in Silverlight. Alas, no . . . at least not by adding a service reference to my web service in the Silverlight app. I get a message stating the service needs to be in the same context. I don’t know what this means, and by this point, I’m quite frustrated with the whole thing, so I’ve taken a bit of a sabbatical from it.

I have a strong suspicion that I’m missing something important. Even trying to adapt the Digg example from Scott Guthrie left me a little bewildered that my own code was having so many troubles. That said, I just found another example from Michael Sync that I’m going to take a look at. It looks a lot more thorough at first glance. I’ll blog about my results with this soon.

Posted in Silverlight | Tagged: , | 1 Comment »

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 »