Creating a Pivot in background code from Linq feed
I've currently got a class that gets and dispatches an XML feed using Linq to XML to a ListBox in my XAML page. I took this from a tutorial, and was wondering, would I be able to make it appear in a pivot?
My idea is to load the feed, and create a pivot page just in background code for each item (Something like, foreach item in my data, create a new pivot, with other content)
Is this possible? I currently get data into a ListBox by Binding the loading and using "TextBlock Text="{Binding Id}"/>" in XAML, and loading the feed in the background code as follows:
myFeed.LoadFeed(//name of the listbox that currently has to exist in XAML)
Here is my code that loads the XML feed and dispatches to a Listbox
public class FeedItem
{
public string Id { set; get; }
public string Text { set; get; }
}
public cl开发者_如何学JAVAass 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; });
}
}
What can be used to hold the data so it can go in a pivot? How do i parse the response item-by-item, into a new pivot?
Yes, you can. You need to provide a datatemplate to the Pivot control. Give attention to the header template which is defined at Pivote level not on the PivotItem's one.
<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>
And the code-behind class:
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; }
}
精彩评论