windows-phone-7pivotsyndication-item

Changing ListBox feeds into Pivot (SyndicationItem message instead of text)


So, I found a way of creating RSS feed app for Windows Phone. It binds SyndicationFeed items into Listbox template. And it works great! However, I wanted to make an app, that handles a bit different. I thought, that changing Listbox template into Pivot would be quite easy. The problem is, that instead of articles I get only "System.ServiceModel.Syndication.SyndicationItem".

I am out of ideas how to fix that, could anyone help me?

Xaml:

mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"  Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">

    <Popup Name="myPopup" IsOpen="True" Width="Auto">
        <ProgressBar Height="Auto" IsIndeterminate="True"  Width="480" />
    </Popup>

    <controls:Pivot Name="FeedPivot" Loaded="loadFeed">
        <controls:Pivot.Title>
            <TextBlock FontSize="56"> Komorkomania </TextBlock>
        </controls:Pivot.Title>

        <controls:Pivot.HeaderTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock>test1</TextBlock>
                 </Grid>
            </DataTemplate>
        </controls:Pivot.HeaderTemplate>

        <controls:Pivot.DataContext>
            <DataTemplate>
                <StackPanel>
                    <TextBlock>test</TextBlock>
                    <!--
                    <TextBlock FontWeight="Bold" FontSize="28" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
                    <TextBlock Name="feedSummary" FontSize="24" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
                    <TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" Width="430" />
                    -->
                 </StackPanel>
            </DataTemplate>
        </controls:Pivot.DataContext>

      </controls:Pivot>
</Grid>

Code behind:

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 Microsoft.Phone.Controls;
using System.IO;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Windows.Controls.Primitives;

namespace KomorkomaniaRss
{
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        myPopup.IsOpen = true;
    }

    // Co robimy jak już w końcu się ściągnie?
    private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(e.Error.Message);
            });
        }
        else
        {
            // Save the feed into the State property in case the application is tombstoned. 
            this.State["feed"] = e.Result;

            UpdateFeedList(e.Result);
            myPopup.IsOpen = false;
        }
    }

    // This method determines whether the user has navigated to the application after the application was tombstoned.

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // First, check whether the feed is already saved in the page state.
        if (this.State.ContainsKey("feed"))
        {
            // Get the feed again only if the application was tombstoned, which means the ListBox will be empty.
            // This is because the OnNavigatedTo method is also called when navigating between pages in your application.
            // You would want to rebind only if your application was tombstoned and page state has been lost. 
            if (FeedPivot.Items.Count == 0)
            {
                UpdateFeedList(State["feed"] as string);
            }
        }
    }

    // This method sets up the feed and binds it to our ListBox. 
    private void UpdateFeedList(string feedXML)
    {
        // Load the feed into a SyndicationFeed instance.
        StringReader stringReader = new StringReader(feedXML);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            // Bind the list of SyndicationItems to our ListBox.
            FeedPivot.ItemsSource = feed.Items;

        });
    }

    public void feedLoader()
    {
        myPopup.IsOpen = true;
        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new System.Uri("http://komorkomania.pl/feed/rss2"));
    }

    private void runBrowser(object sender)
    {
        ListBox listBox = sender as ListBox;

        if (listBox != null && listBox.SelectedItem != null)
        {
            // Get the SyndicationItem that was tapped.
            SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;

            // Set up the page navigation only if a link actually exists in the feed item.
            if (sItem.Links.Count > 0)
            {
                // Get the associated URI of the feed item.
                Uri uri = sItem.Links.FirstOrDefault().Uri;

                // Create a new WebBrowserTask Launcher to navigate to the feed item. 
                // An alternative solution would be to use a WebBrowser control, but WebBrowserTask is simpler to use. 
                WebBrowserTask webBrowserTask = new WebBrowserTask();
                webBrowserTask.Uri = uri;
                webBrowserTask.Show();
            }
        }

    }

    // The SelectionChanged handler for the feed items 
    private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        runBrowser(sender);
    }

    private void loadFeed(object sender, RoutedEventArgs e)
    {
        feedLoader();
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }
    }
}
}

There are some "leftovers" for handling another actions. Please ignore them, I didn't get to it yet. The biggest focus is to make it show text after app is loaded.


Solution

  • I bealive that I fixed that. There was a problem with Pivot concept. This is how I fixed that:

            <controls:Pivot Name="FeedPivot" Loaded="loadFeed" ScrollViewer.VerticalScrollBarVisibility="Visible" Tap="feedPivotTap" Margin="0,88,0,0" LoadedPivotItem="getPivotItem">
    
             <controls:Pivot.HeaderTemplate>
                <DataTemplate>
                        <TextBlock FontWeight="Bold" FontSize="28" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="#FF5DBA00" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
                    </DataTemplate>
            </controls:Pivot.HeaderTemplate>
    
            <controls:Pivot.ItemTemplate>
                <DataTemplate>
                    <ScrollViewer>
                         <StackPanel>
                            <TextBlock Name="feedSummary" FontSize="24" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
                            <TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" Width="430" />    
                          </StackPanel>
                    </ScrollViewer>
                </DataTemplate>     
            </controls:Pivot.ItemTemplate>
    
        </controls:Pivot>