Silverlight中数据与通信之WCF
1. 新建一个实体类接口文件IBlog.cs
// 注意: 如果更改此处的接口名称 "IBlog",也必须更新 Web.config 中对 "IBlog" 的引用。
[ServiceContract]
public interface IBlog
{
[OperationContract]
Post[] Getposts();
}
[DataContract]
public class Post
{
public Post(int ID,string Title,string author)
{
this.ID = ID;
this.Title = Title;
this.Author = author;
}
[DataMember]
public int ID { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Author { get; set; }
}
2. 新建一个实体类文件Blog.svc
// 注意: 如果更改此处的类名 "Blog",也必须更新 Web.config 中对 "Blog" 的引用。
//注意:web.config中的binding默认的是wshttpBinding,需修改为basichttpBinding,
//<endpoint address="" binding="basichttpBinding" contract="Binglang.SilverlightDemo9.Web.IBlog">
public class Blog : IBlog
{
public Post[] Getposts()
{
List<Post> posts = new List<Post>()
{
new Post(1,"一步一步学Silverlight 2系列(13):数据与通信之WebRequest","TerryLee"),
new Post(2,"一步一步学Silverlight 2系列(12):数据与通信之WebClIEnt",
new Post(3,"一步一步学Silverlight 2系列(11):数据绑定",
new Post(4,"一步一步学Silverlight 2系列(10):使用用户控件",
new Post(5,"一步一步学Silverlight 2系列(9):使用控件模板",
new Post(6,"一步一步学Silverlight 2系列(8):使用样式封装控件观感","TerryLee")
};
return posts.ToArray();
}
}
2. 调用
前台代码:
<ListBox x:name="posts" 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 Title}" Height="40"></TextBlock>
<TextBlock Text="{Binding Author}" Height="40" Foreground="Orange"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后台代码:
Loaded+=new RoutedEventHandler(MainPage_Loaded);
//实现调用wcf,并进行数据的绑定。采用异步模式。过程与调用webService通信差不多,只不过需要指定Bingding等信息。
private voID MainPage_Loaded(object sender,RoutedEventArgs e)
{
BasichttpBinding binding = new BasichttpBinding();
EndpointAddress endPoint = new EndpointAddress("http://localhost:52424/Blog.svc");
BlogClIEnt clIEnt = new BlogClIEnt(binding,endPoint);
clIEnt.GetpostsCompleted += new EventHandler<GetpostsCompletedEventArgs>(clIEnt_GetpostsCompleted);
clIEnt.GetpostsAsync();
}
voID clIEnt_GetpostsCompleted(object sender,GetpostsCompletedEventArgs e)
{
if (e.Error == null)
{
posts.ItemsSource = e.Result;
}
}
总结以上是内存溢出为你收集整理的SilverLight中数据与通信之WCF全部内容,希望文章能够帮你解决SilverLight中数据与通信之WCF所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)