[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; } }
[ServiceContract] public interface IBlog { [OperationContract] Post[] GetPosts(); }
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","TerryLee"), new Post(3,"一步一步学Silverlight 2系列(11):数据绑定","TerryLee"), new Post(4,"一步一步学Silverlight 2系列(10):使用用户控件","TerryLee"), new Post(5,"一步一步学Silverlight 2系列(9):使用控件模板","TerryLee"), new Post(6,"一步一步学Silverlight 2系列(8):使用样式封装控件观感","TerryLee") }; return posts.ToArray(); } }
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="TerryLee.SilverlightDemo27Web.BlogBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="TerryLee.SilverlightDemo27Web.BlogBehavior" name="TerryLee.SilverlightDemo27Web.Blog"> <endpoint address="" binding="basicHttpBinding" contract="TerryLee.SilverlightDemo27Web.IBlog"> </endpoint> </service> </services> </system.serviceModel>
<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="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> </Grid>
public partial class Page : UserControl { public Page() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { Binding 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; } } }
本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67251
本文出自 51CTO.COM技术博客