从Linq feed创建背景代码中的Pivot

时间:2011-05-01 09:56:35

标签: c# xaml windows-phone-7

我目前有一个类,它使用Linq to XML获取XML调度并将其发送到XAML页面中的ListBox。我从一个教程中得到了这个,并且想知道,我是否能够将它显示在一个支点中?

我的想法是加载Feed,并在每个项目的后台代码中创建一个数据透视页面(类似于我的数据中的foreach项目,创建一个新的数据透视图,以及其他内容)

这可能吗? 我目前通过绑定加载并使用“TextBlock Text =”{Binding Id}“/>”将数据导入ListBox在XAML中,并在后台代码中加载feed,如下所示:

myFeed.LoadFeed(//name of the listbox that currently has to exist in XAML)

以下是我的代码,用于加载XML Feed并将其分派到Listbox

public class FeedItem
{
    public string Id { set; get; }
    public string Text { set; get; }

}

public class Feed
{
    ListBox myContext;

    public void LoadFeed(ListBox context)
    {
        myContext = context;
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://myDataSource"));
        request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
    }


    private static readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
    private void ReadCallback(IAsyncResult asynchronousResult)
    {


        HttpWebRequest request =
            (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response =
          (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        XDocument doc = XDocument.Load(response.GetResponseStream());

           List<FeedItem> feedItems = (from question in doc.Descendants(m + "properties")
                                        select new FeedItem()
                                        {
                                            Id = question.Descendants().ToList()[0].Value,
                                            Text = question.Descendants().ToList()[1].Value
                                        }).ToList();

            myContext.Dispatcher.BeginInvoke(() => { myContext.ItemsSource = feedItems; });






    }
}

什么可以用来保存数据,以便它可以进入枢轴? 如何逐项解析响应到新的轴?

1 个答案:

答案 0 :(得分:0)

是的,你可以。您需要为Pivot控件提供datatemplate。注意在Pivote级别定义的标题模板,而不是PivotItem的标题模板。

<Grid x:Name="LayoutRoot" Background="Transparent">
<controls:Pivot ItemsSource="{Binding MyPivots}">
    <controls:Pivot.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding MyTitle}"/>
        </DataTemplate>
    </controls:Pivot.HeaderTemplate>
    <controls:Pivot.ItemTemplate>
        <DataTemplate>
            <controls:PivotItem>                       
                <TextBlock Text="{Binding YourRssText}" />
            </controls:PivotItem>
        </DataTemplate>
    </controls:Pivot.ItemTemplate>
</controls:Pivot>

代码隐藏类:

public partial class MainPage : PhoneApplicationPage{
     public List<RssFeed> MyPivots { get; set; }
    // Constructor
    public MainPage()
    {
    MyPivots = new List<RssFeed>
    {
        new RssFeed{ MyTitle = "Title1", YourRssText = "Body1"},
        new RssFeed{ MyTitle = "Title2", YourRssText = "Body2"},
        new RssFeed{ MyTitle = "Title3", YourRssText = "Body3"},
    };
    InitializeComponent();
    this.DataContext = this;

     }
}


public class RssFeed   
{   
   public string MyTitle { get; set; }

   public string YourRssText { get; set; }
 }