Silverlight and webservices

First download the binaries you need from here:
http://silverlight.net/GetStarted/

Next run VS2008 and create a new project.

Select the Silverlight node and then the Silverlight Application node.

ProjectName = MySilverlightApp
Tick the checkbox "Create directory for solution"
Click OK

On the wizard page you want the default values:
* Add a new page to the solution for hosting the control
Project type = Web Site
Name = MyWebService

Now delete the two ASPX files, we wont be needing those.

Rename the HTML page to Index.html and set it as the project start page.

Right click the website project and select "Add new item".

Select "Web Service".
Name = DateTimeService.asmx
Click ADD

Change the HelloWorld method to

public DateTime GetServerDateTime()
{
  return DateTime.Now;
}


Right-click the References node on the Silverlight project and select "Add service reference".
Click the "Discover" button, and in the tree view that appears select the DateTimeServer node.
Set the NameSpace to DateTimeServer
Click OK

Now open Page.xaml and enter the following within the <Grid>

<StackPanel>
  <Button x:Name="ButtonGetServerDateTime" Content="Get date time" Click="ButtonGetServerDateTime_Click"/>
  <TextBlock x:Name="TextBlockDateTime" Text="Ready..."/>
</StackPanel>


Note: When you type Click=" you will get the option to hit <TAB> to implement the event handler.

Now open Page.xaml.cs

Add the following private member to the class:
private DateTimeServer.DateTimeServiceSoapClient proxy;


Initialise it in the page’s constructor:
proxy = new MySilverlightApp.DateTimeServer.DateTimeServiceSoapClient();
proxy.GetServerDateTimeCompleted +=
new EventHandler<MySilverlightApp.DateTimeServer.GetServerDateTimeCompletedEventArgs>(proxy_GetServerDateTimeCompleted);


Implement the "Completed" method like so:
TextBlockDateTime.Text = e.Result.ToString();


Finally implement the ButtonServerDateTime_Click method like so:
private void GetServerDateTime_Click(object sender, RoutedEventArgs e)
{
  proxy.GetServerDateTimeAsync();
}


That’s it! Run the app and click the button!

Comments

Popular posts from this blog

Connascence

Convert absolute path to relative path

Printing bitmaps using CPCL