Silverlight 2 (beta1)数据操作(2)——使用ASP.NET Web Service进行数据CRUD操作(下)
2008-10-11 11:42:49 来源:WEB开发网第一步:在Silverlight工程中添加引用
第二步:查找System.Windows.Controls.Data程序集,添加进来
第三步:在UserControl中添加这个引用,有智能感知。我将其命名为Data。
在前台编写代码如下
<Button x:Name="reButton" Content="刷新"
后台代码
Width="50" Height="30" Grid.Row="0"></Button>
<Data:DataGrid x:Name="userDataGrid" Height="200"
Width="700" Margin="0,5,0,10"
AutoGenerateColumns="True"
VerticalAlignment="Top" Grid.Row="1">
</Data:DataGrid>//显示数据
修改数据部分前台界面
void ListingControlDisplay(object sender, RoutedEventArgs e)
{
WebServiceProxy.UserManageSoapClient userMgrSoapClient =
new YJingLee.WebSrv.WebServiceProxy.UserManageSoapClient();
userMgrSoapClient.RetrieveUsersAsync();
userMgrSoapClient.RetrieveUsersCompleted +=
new EventHandler<YJingLee.WebSrv.WebServiceProxy.
RetrieveUsersCompletedEventArgs>
(userMgrSoapClient_RetrieveUsersCompleted);
}
void userMgrSoapClient_RetrieveUsersCompleted(object sender,
YJingLee.WebSrv.WebServiceProxy.RetrieveUsersCompletedEventArgs e)
{
if (e.Error == null)
displayData(e.Result);
}
private void displayData(string xmlContent)
{
try
{
if (xmlContent != string.Empty)
{
XDocument xmlUsers = XDocument.Parse(xmlContent);
var users = from user in xmlUsers.Descendants("User")
select new
{
UserID = Convert.ToInt32
(user.Element("UserID").Value),
UserName = (string)
user.Element("UserName").Value
};
List<User> usersList = new List<User>();
foreach (var u in users)
{
User use = new User
{ UserID = u.UserID, UserName = u.UserName };
usersList.Add(use);
}
userDataGrid.ItemsSource = usersList;
}
else
{
userDataGrid.ItemsSource = null;
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
}
Tags:Silverlight 数据
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接