Tuesday, 28 October 2008

[update: source included
I have included the source. The source code works with the asp.net developer webserver instead of IIS. That will make it easier for you to run the sample!]

The autocomplete box is a control that will popup a combobox based on what the user is typing. You see a lot of these on the web, and for good reason: they really help make your enduser more productive!
In this post I will walk you through building a (WCF) webservice and connecting it up to the box. As you will see, it's really easy.

If you are unable to visualize it, this picture from our codeplex pages demonstrates a simple example:

AutoCompleteBox example

Step 1: create the webservice

We start of by creating a new project and choosing to create a new asp.net project for us. It will hold our webservice. Let's add one by choosing the 'Silverlight enabled WCF Service' template that should be when you add a new item.

[sample uses asp.net web server]

Setting everything up correctly:
You will need to install IIS and most of the (non-default) settings. Visual studio will complain if you are not setup correctly and tell you which features it lacks.
What bites many people that install IIS after they install .Net is that WCF isn't registered correctly. You can do so by issuing the 'ServiceModelReg /r' command. Find it here: %SystemRoot%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\

Do run visual studio as an administrator. You will get an error saying that the client can not be found, if you don't.

Lastly: you will not need any crossdomain xml definitions because you are running from the asp.net server. Do not forget to add one, if you are publishing your project though!!

So, add a service called 'NamesService.svc'.
After you have made a list of your friends, return them in a similar fashion as I do:

image

(Actually, after watching the PDC keynotes, don't you really want to use livemesh to do this kinds of stuff?? Or could we get this from Bluehoo? )

Step 2: connecting from silverlight

In your silverlight application, add a service reference. Because you are working from one big happy solution, you will be able to press the discover button and get your service. Add it and give it a nice name.

image

This will add a proxy to your Silverlight client.

Intermezzo:
People that have read this blog for some time, know I'm not very impressed with all the autogenerated proxy features. I do not feel generating your proxy gives you enough power over what you are sending over the wire. Also, I might want to do crazy stuff in my proxy. However, Silverlights data capabilities are pretty restricted and I will admit that the VS team has done some great integration here. All in all, this runs out of the box without any hickups.

Read my articles about EntityFramework Poco implementations which does a lot of WCF customization if you want to know more.

Step 2.1: test it out, man!

Let's just quickly see if we can get our service to work! Add these lines of code in your page.xaml.cs file and run.

image

This should print out the number 7 in your output.

We are first setting up a callback that will be called when the webservice returns with information. The args.Result property is a strongly typed property which holds your result.
We kick of the call to the webservice with our proxy.GetFriendsAsync() call.

Step 3: bring in the autocomplete textbox

It is time to add the autocomplete textbox in the mix.

Add a reference to the silverlight toolkit to your project, and (do) use the 'controls' prefix as the xmlns namespace. Then, just add it to the page xaml.

image

I have added the box and have added an eventhandler to the Populating event. This will allow me intercept and call our webservice. Dropping back to code, we'll hook everything up:

image

On line 33, I'm cancelling the population. Populating should really be done synchronously and fast. Since we can't do it fast, we will be doing the populating ourselves.
On line 50, we make the call to our webservice. When it is finished, it will execute the anonymous delegate on lines 39 to 46.
Our args.Result is a string[] with friends from the server. I will combine that with a list of friends we might keep in a different source.

The result:

image

 

Check out the LiveSearch scenario sample that comes with the toolkit. It connects to livesearch and fetches search suggestions on the fly.

AutoCompleteBoxToWebserviceProject.zip (159.35 KB)
Tuesday, 28 October 2008 23:33:41 (Romance Standard Time, UTC+01:00)  #    Comments [28]  |  Trackback

When you are browsing the charting samples here, you will notice a scenario that is currently called 'Zoom'.

unzoomedChart

I think it's pretty clear that you are supposed to drag the zoom slider :)

When you do, the chart is resized and to look like this:

ZoomedChart

Charts do not have this capability built-in at the moment, so this gives us the opportunity to dig into the charts a bit.

All charts are of the same type (Chart) and you can create a line series by adding an instance of LineSeries to the chart. This is quite flexible, because you can add as many as you want. It our case, two are added. Let's take a look at the different controls that make up this sample:

Zoomchart xaml

As you can see, two LineSeries have been added, their ItemsSource pointing to a static resource defined somewhere else.

The slider is hooked up to a method in my codebehind which executes this one line of code:

            ChartArea.Width = ScrollArea.ViewportWidth + (ScrollArea.ViewportWidth * zoom / 100.0);

I am not setting the width of the complete chart. If I were to do that, the legend and title would scroll away as well, which is not what I want. Besides, there is no ScrollViewer around to handle the scrolling. Lets take a look at the custom template that does have a ScrollViewer in it:

image

I have retemplated the chart to include a ScrollViewer.

You can clearly see that the legend is not in the ChartArea. So, setting the width of the ChartArea will give you the zoom you are aiming for. The axis get notified of the change and (if set to 'Auto'), will update to show more or less information.

Since Chart is a sealed class, I had to walk the visual tree to get to both the ChartArea component and the ScrollViewer.

Tuesday, 28 October 2008 20:48:51 (Romance Standard Time, UTC+01:00)  #    Comments [16]  |  Trackback

Today, Scott Guthrie announced our teams product: the Silverlight Toolkit. My boss, Shawn Burke just posted a blog that lists all the different controls that are included and gives out information about the quality levels of the controls. It's a must-read.

The components included in this release are:

Components in the Preview Quality Band

Components in the Stable Quality Band

I joined only a few weeks ago and have been looking at the controls. What is most impressive, is the incredible amount of unittests that are included with this release. On the team is Jeff Wilcox who heads the Silverlight Unit Test Framework.

I have been playing around with the controls and will probably be blogging some short blobs about my findings. I've spend quite some time with charting.

David Anson (Delay) heads up the charting control, which is the biggest feature that we are including. He has just announced it with a very detailed introduction post. He is also announcing the ChartBuilder application, that will make it easy for you to play around with the settings.

Go download and play, let us know what you think!

Tuesday, 28 October 2008 19:18:40 (Romance Standard Time, UTC+01:00)  #    Comments [7]  |  Trackback
 Sunday, 19 October 2008

I’m very excited that Silverlight 2 has been released. I’m too new to Microsoft to claim even the slightest involvement, but it’s wonderful to see the excitement both within Microsoft as outside.
It will be interesting to see what will happen with the (already great) uptake of Silverlight by the market.

As I am spending most of my days knee-deep in Xaml nowadays, I always try to find things that will help me be more productive. It happens quite often that I want to select a complete xaml tag. It’s way too much effort to use the mouse to select it, so I often use the control-m-m shortcut to collapse a tag and then select it. However, a few days ago I took 5 minutes to automate this.

image

When you put your mouse somewhere in the Grid tag, and use my macro, you end up with this:

image

You can even put your mouse in the endtag. I bound it to Control-Q and it has made my life that much better!

The macro is as simple as this:

    Sub SelectXMLTagContents()
        DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstColumn)
        DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.EndOfLine(True)
        DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
End Sub

It collapses a tag, jumps to the first column, selects the line and then does an uncollapse.

For those using a tool like Karl’s ‘Xaml Power Toys’, it might also be a worthwile addition to their shortcuts.

Sunday, 19 October 2008 21:17:36 (Romance Standard Time, UTC+01:00)  #    Comments [26]  |  Trackback
 Friday, 03 October 2008

I’ll slowly try to start blogging again :)  It’s been extremely hectic, and I am slowly ramping up. Lately I’ve not been blogging or been just scribbling non-technical jibberish here. Let’s get back on the train!

Loading assemblies in Silverlight can be a bit hard because everything is packaged up in a Xap file. If you want to get to all the assemblies and start instantiating types, you’ll quickly run into a wall.

However, Windows is about living without walls, and thus there is a workaround! :)

  0    string assemblyname = "System.Windows.Controls";
  1
  2    AssemblyPart part = Deployment.Current.Parts.SingleOrDefault(asmpart => asmpart.GetValue(FrameworkElement.NameProperty).ToString() == assemblyname);
  3
  4    StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri(part.Source, UriKind.Relative));
  5    Assembly asm = part.Load(streamInfo.Stream);
  6

It appears there is a very nice class available: Deployment. It is a list of the deployment section of your App.Manifest. The manifest describes all the ‘DeploymentParts’ in the Xap file. It thus also describes the filenames of the dll’s that are included.

If you know the dll name, that is great, and you can easily load it; skip to line 4.
In my case, I just had the assembly name (which does not mean I have the dll name) and on line 2 you can see me running through the parts that are available to me. The x:Name in the deployment file is the name of my assembly, so I get to it by using the DependencyProperty system: querying for the FrameworkElement.NameProperty.

Once I have an assembly part, I have the actual filename: part.Source. So, on line 4, I can now setup a stream to it, and on line 5 I can easily load my assembly.

From this point on, I can do everything you would expect.

Happy sailing!

Friday, 03 October 2008 07:55:38 (Romance Standard Time, UTC+01:00)  #    Comments [12]  |  Trackback