Silverlight 2 (beta1)数据操作(4)——调用WCF进行数据CRUD操作
2008-10-11 11:42:33 来源:WEB开发网同创建数据类似,调用服务并读取数据。
void retrieveButton_Click(object sender, RoutedEventArgs e)
{
//注册RetrieveUserCompleted事件
userSvcClient.RetrieveUserCompleted +=
new EventHandler<YJingLee.WCF.UserService.
RetrieveUserCompletedEventArgs>
(userSvcClient_RetrieveUserCompleted);
//调用RetrieveUserAsync()方法查询用户
userSvcClient.RetrieveUserAsync();
}
void userSvcClient_RetrieveUserCompleted(object sender,
YJingLee.WCF.UserService.RetrieveUserCompletedEventArgs 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);
}
UserList.ItemsSource = usersList;
}
else
{
UserList.ItemsSource = null;
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
3.更新数据
Tags:Silverlight 数据
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接