Silverlight 中使用WebServer
Page.xaml
<UserControl x:Class="SilverlightApplication15.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
>
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<data:DataGrid x:Name="dgrd_DataGrid" Margin="5" AutoGenerateColumns="False" >
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="姓名" Width="150"
Binding="{Binding UserName}">
</data:DataGridTextColumn>
<data:DataGridTextColumn Header="年龄" Width="150"
Binding="{Binding UserAge}">
</data:DataGridTextColumn> "
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
</UserControl>
Page.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 System.Collections.ObjectModel;
namespace SilverlightApplication15
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
void GetUser()
{
UserService.Service1SoapClient userService = new SilverlightApplication15.UserService.Service1SoapClient();
userService.GetUserCompleted += new EventHandler<SilverlightApplication15.UserService.GetUserCompletedEventArgs>(userService_GetUserCompleted);
userService.GetUserAsync();
}
void userService_GetUserCompleted(object sender, SilverlightApplication15.UserService.GetUserCompletedEventArgs e)
{
if (e.Error == null)
{
List<UserTest> user = new List<UserTest>();
ObservableCollection<SilverlightApplication15.UserService.User> list = e.Result;
for (int i = 0; i < list.Count; i++)
{
user.Add(new UserTest
{
UserName = list[i].UserName,
UserAge = list[i].UserAge
});
}
this.dgrd_DataGrid.ItemsSource = user;
}
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
GetUser();
}
}
}
WebService中仅包含一个返回List<User>的GetUser();
PS:在Silverlight中并不包含DataGrid,如需使用,请引用System.Windows.Controls.Data
并且在Page.xaml中声明
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"