将列表框更改为枢轴(SyndicationItem消息而不是文本)



所以,我找到了一种为Windows Phone创建RSS feed应用程序的方法。它将SyndicationFeed项目绑定到ListBox模板中。而且效果很好!但是,我想制作一个应用程序,该应用程序有些不同。我认为,将列表框模板变成枢轴非常容易。问题是,我只会获得" System.ServiceModel.Syndication.syndicationItem"。

而不是文章。

我没有想法如何解决这个问题,有人可以帮我吗?

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>

代码背后:

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();
        }
    }
}
}

有一些"剩菜"来处理其他动作。请忽略他们,我还没有做到。最大的重点是在加载应用程序后将其显示为文本。

我修复了它。枢轴概念存在问题。这就是我解决的方式:

        <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>

最新更新