silverlight和WebService的简单应用

功能实现显示学生的信息:

 

Student::

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SilverlightApplication3.Web
{
    public class Student
    {
        public string ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }

        public string Dep
        {
            get;
            set;
        }
    }
}


 

新建一个Web服务:

 public class StudentService : System.Web.Services.WebService
    {

        [WebMethod]
        public Student[] GetStudent()
        {
            List<Student> students = new List<Student>()
           {
               new Student{ID="1",Name="刘涛",Age=21,Dep="Computer"},
               new Student{ID="2",Name="张华",Age=22,Dep="Computer"},
               new Student{ID="3",Name="程勇",Age=21,Dep="Computer"},
               new Student{ID="4",Name="李建",Age=23,Dep="Computer"}
           };
            return students.ToArray();
        }
    }


 

 

在silverlight项目中引用上面的服务:

MainPage.xaml

<UserControl x:Class="SilverlightApplication3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">

    <Grid Background="#46461F">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></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="最新随笔" Foreground="White"
                   HorizontalAlignment="Left" VerticalAlignment="Center"
                   Margin="20 0 0 0"></TextBlock>
        </Border>
        <ListBox x:Name="Students" 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 Name}" Height="40"></TextBlock>
                        <TextBlock Text="{Binding Age}" Height="40" Foreground="Orange"></TextBlock>
                        <TextBlock Text="{Binding Dep}" Height="40" Foreground="Orange"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>


 

 

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication3.StudentService;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            StudentServiceSoapClient client = new StudentServiceSoapClient();
            client.GetStudentCompleted += new EventHandler<GetStudentCompletedEventArgs>(client_Completed);
            client.GetStudentAsync();
        }

        private void client_Completed(object sender,GetStudentCompletedEventArgs e)
        {
            if(e.Error==null)
            {
                Students.ItemsSource= e.Result;
            }
        }

    }
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值