[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:

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:
(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.
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.
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.
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:
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:
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)