功能实现显示学生的信息:
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;
}
}
}
}