One-step learning of Silverlight 2 series (15): ASMX for data and communication

Source: Internet
Author: User
Overview

The release of Silverlight 2 Beta 1 brings us a lot of surprises from Runtime and Tools, such as supporting the framework languages Visual Basic, Visual C #, IronRuby, Ironpython, A series of new features such as JSON, Web Service, WCF, and Sockets support. The article "one-step learning Silverlight 2 series" takes you to Silverlight 2 development quickly from the following aspects: Silverlight 2 basic knowledge, data and communication, custom controls, animation, and graphic images.

This article briefly introduces how to communicate with ASMX in Silverlight 2.

Simple Example

The example in this article is very simple, and the process is similar to learning Silverlight 2 series (14) in step by step: data and communication in WCF, we still display a list of the latest essays, the final result is as follows:

Define a business entity Post.

public class Post{    public int Id { get; set; }    public string Title { get; set; }    public string Author { get; set; }}

Add a Web Service file in a Web project named BlogService. asmx

Implement the service and define a GetPosts method:

Public class BlogService: WebService {[WebMethod] public Post [] GetPosts () {List <Post> posts = new List <Post> () {new Post {Id = 1, title = "one-step Silverlight 2 series (13): WebRequest for data and communication", Author = "TerryLee"}, new Post {Id = 2, title = "one-step Silverlight 2 series (12): WebClient for data and communication", Author = "TerryLee"}, new Post {Id = 3, title = "one-step Silverlight 2 series (11): Data Binding", Author = "TerryLee"}, new Post {Id = 4, title = "one-step Silverlight 2 series (10): using user controls", Author = "TerryLee"}, new Post {Id = 5, title = "one-step Silverlight 2 series (9): using the control template", Author = "TerryLee"}, new Post {Id = 6, title = "one-step Silverlight 2 series (8): style encapsulation controls", Author = "TerryLee" }}; return posts. toArray ();}}

Similarly, set the port number of the Web Development Server to a fixed value. Set it to 8081 here, and then test whether the service is correct in the browser:

Click "call" to test whether the service is correct.

In the Silverlight project, add a reference to the service,

Use the Object Browser to view the objects in the client proxy class:

Compile the presentation interface. The XAML is as follows, as shown in the previous example:

<Grid Background = "# 46461F"> <Grid. rowDefinitions> <RowDefinition Height = "40"> </RowDefinition> <RowDefinition Height = "*"> </RowDefinition> </Grid. rowDefinitions> <Grid. columnDefinitions> <ColumnDefinition> </Grid. columnDefinitions> <Border Grid. row = "0" Grid. column = "0" CornerRadius = "15" Width = "240" Height = "36" Background = "Orange" Margin = "20 0 0 0" HorizontalAlignment = "Left"> <textBlock Text = "latest essay" Foreground = "White" HorizontalAlignment = "Left" verticalignment = "Center" Margin = "20 0 0 0"> </TextBlock> </Border> <listBox x: name = "Posts" Grid. row = "1" Margin = "40 10 10 10"> <ListBox. itemTemplate> <DataTemplate> <StackPanel Orientation = "Horizontal"> <TextBlock Text = "{Binding Id}" Height = "40" Foreground = "Red"> </TextBlock> <TextBlock text = "{Binding Title}" Height = "40"> </TextBlock> <TextBlock Text = "{Binding Author}" Height = "40" Foreground = "Orange"> </TextBlock> </StackPanel> </DataTemplate> </ListBox. itemTemplate> </ListBox> </Grid>

To call ASMX and bind data. The asynchronous mode is still used. The method used is shown in the red box. The process is similar to that of WCF communication, but you do not need to specify Bingding and other information:

public partial class Page : UserControl{    public Page()    {        InitializeComponent();    }    private void UserControl_Loaded(object sender, RoutedEventArgs e)    {        BlogServiceSoapClient client = new BlogServiceSoapClient();        client.GetPostsCompleted += new EventHandler<GetPostsCompletedEventArgs>(client_GetPostsCompleted);        client.GetPostsAsync();    }    void client_GetPostsCompleted(object sender, GetPostsCompletedEventArgs e)    {        if (e.Error == null)        {            Posts.ItemsSource = e.Result;        }    }}

The following is an example of calling ASMX in Silverlight 2:

Conclusion

This article briefly introduces how to call ASMX in Silverlight 2. You can download the sample code from here.

Next article: Learn Silverlight 2 series (16) step by step: JSON for data and communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.