このクイックスタートでは、配信サービスへのアクセスや表示が可能な Silverlight クライアントを構築し、RSS 2.0 または Atom 1.0 配信フィードを公開する方法について説明します。
このクイックスタートは、次のセクションで構成されています。
メモ
Silverlight の配信機能は、その大部分が Windows Communication Foundation (WCF) オブジェクト モデルをベースとしています。WCF の配信機能セットの詳細については、MSDN ライブラリにある「WCF 配信の概要」を参照してください。
Silverlight クライアントによる配信フィードへのアクセス
Silverlight で配信項目にアクセスするには、System.Net.WebClient オブジェクトを使用してフィードから文字列を非同期にダウンロードし、System.Xml.XmlReader を使用して文字列を読み取ります。次の例では、『Wired』誌のトップ記事を提供する配信フィードにアクセスしています。このサンプルを実行するには、[Click Here for the Feed] (フィードの取得) をクリックしてください。配信項目の一覧がボタンの下に表示されます。
Page.xaml ファイル (Page.xaml.cs または Page.xaml.vb) 内の次のマネージ コードは、フィードの配信項目にアクセスする方法を示します。Uri は、アクセスするフィードのアドレスを表します。このアドレスは、『Wired』誌の配信フィードを指しています。
C#
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.ServiceModel.Syndication; using System.Xml; using System.IO;
namespace SyndicationFeedReader { public partial class Page : UserControl { public Page() { InitializeComponent(); }
void OnClick(object sender, EventArgs args) { WebClient client = new WebClient(); client.DownloadStringAsync( new Uri("http://feeds.wired.com/wired/index?format=xml")); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(request_DownloadStringCompleted); }
void request_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { XmlReader reader = XmlReader.Create(new StringReader(e.Result)); SyndicationFeed feed = SyndicationFeed.Load(reader); lbSyndicationItems.ItemsSource = feed.Items; }
} }
Visual Basic
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Net Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Documents Imports System.Windows.Input Imports System.Windows.Media Imports System.Windows.Media.Animation Imports System.Windows.Shapes Imports System.ServiceModel.Syndication Imports System.Xml Imports System.IO
Namespace SyndicationFeedReader Public Partial Class Page Inherits UserControl Public Sub New() InitializeComponent() End Sub
Private Sub OnClick(sender As Object, args As EventArgs) Dim client As New WebClient() client.DownloadStringAsync( New Uri("http://feeds.wired.com/wired/index?format=xml")) client.DownloadStringCompleted += New DownloadStringCompletedEventHandler(request_DownloadStringCompleted) End Sub
Private Sub request_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs) Dim reader As XmlReader = XmlReader.Create(New StringReader(e.Result)) Dim feed As SyndicationFeed = SyndicationFeed.Load(reader) lbSyndicationItems.ItemsSource = feed.Items End Sub
End Class End Namespace
配信フィードの表示
フィード タイトルを表示するには、フィード トピックのタイトルを TextBlock の Text プロパティにバインドします。
XAML
<UserControl x:Class="SyndicationFeedReader.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="600" Height="500"> <Grid x:Name="LayoutRoot" Background="Blue"> <Grid.RowDefinitions> <RowDefinition Height="100"/> <RowDefinition /> </Grid.RowDefinitions> <Button Grid.Row="0" Margin=" 5" Height="60" Content="Click Here for the Feed" Click="OnClick"/> <ListBox x:Name="lbSyndicationItems" Margin="4" Grid.Row="1"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Title.Text}"/> </DataTemplate> </ListBox.ItemTemplate>> </ListBox> </Grid> </UserControl>
次のコードは、SyndicationFeed オブジェクトのデータをバインドする方法を示します。
C#
lbSyndicationItems.ItemsSource = feed.Items;
Visual Basic
lbSyndicationItems.ItemsSource = feed.Items
ドメイン間の呼び出し
重要な点として、『Wired』のサイトへの呼び出しがドメイン間の呼び出しであることに注意してください。この例でドメイン間の呼び出しが動作するのは、それを可能にするファイルが『Wired』のフィードに用意されているためです。ドメイン間ポリシー ファイルは、Wired のドメイン間ポリシー ファイルにあります。その他のフィードでも、このようにアクセスする場合は、同様にドメイン間の呼び出しを有効にする必要があります。詳細については、MSDN の Silverlight ドキュメントの「ドメインの境界を越えてサービスを利用できるようにする」を参照してください。
参照
フィードバックを送信する