Windows Phone 8.1 Rss阅读器

MainPage.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,0,0,28">
        <TextBlock Text="Rss阅读器" FontSize="20"  />
        <TextBlock Text="文章列表" FontSize="60" />
    </StackPanel>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <TextBox Header="请输入合法的RSS阅读源的地址:" x:Name="rssURL" Text="http://www.cnblogs.com/rss" />
            <Button Content="加载 RSS" Click="Button_Click" Width="370"/>
            <ListView x:Name="listbox" SelectionChanged="OnSelectionChanged" Height="350">
                <ListView.ItemTemplate  >
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="60" />
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="{Binding Title}" FontSize="25" TextWrapping="Wrap" />
                            <TextBlock Grid.Row="1" Text="{Binding PublishedDate}" FontSize="20" />
                            <TextBlock Grid.Row="2" TextWrapping="Wrap" Text="{Binding PlainSummary}" FontSize="18" Opacity="0.5"/>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>
    </Grid>
</Grid>
public sealed partial class MainPage: Page {
    public MainPage() {
        this.InitializeComponent();
        //this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e) {}

    private async void Button_Click(object sender, RoutedEventArgs e) {
        if (rssURL.Text != "") {
            RssService.GetRssItems(rssURL.Text, async(items) = >{
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () = >{
                    listbox.ItemsSource = items;
                });
            },
            async(exception) = >{
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() = >{
                    await new MessageDialog(exception).ShowAsync();
                });

            },
            null);
        } else {
            await new MessageDialog("请输入RSS地址").ShowAsync();
        }
    }

    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) {
        if (listbox.SelectedItem == null) return;
        var template = (RssItem) listbox.SelectedItem;
        Frame.Navigate(typeof(DetailPage), template);
        listbox.SelectedItem = null;
    }
}

DetailPage.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,0,0,28">
        <TextBlock Text="Rss阅读器" FontSize="20"  />
        <TextBlock Text="{Binding Title}" FontSize="25" TextWrapping="Wrap"/>
    </StackPanel>

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <TextBlock Text="{Binding PublishedDate}" FontSize="15" Opacity="0.5" />
            <TextBlock Text="{Binding Url}" FontSize="15" Opacity="0.5" />
            <ScrollViewer Height="500">
                <TextBlock Text="{Binding PlainSummary}" FontSize="20" TextWrapping="Wrap"/>
            </ScrollViewer>
        </StackPanel>
    </Grid>
</Grid>
public sealed partial class DetailPage: Page {
    public DetailPage() {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e) {
        RssItem item = e.Parameter as RssItem;
        if (item != null) {
            this.DataContext = item;
        }
    }
}
public class RssItem {

    public RssItem(string title, string summary, string publishedDate, string url) {
        Title = title;
        Summary = summary;
        PublishedDate = publishedDate;
        Url = url;
        PlainSummary = WebUtility.HtmlDecode(Regex.Replace(summary, "<[^>]+?>", ""));
    }

    public string Title {
        get;
        set;
    }

    public string Summary {
        get;
        set;
    }

    public string PublishedDate {
        get;
        set;
    }

    public string Url {
        get;
        set;
    }

    public string PlainSummary {
        get;
        set;
    }
}

public static class RssService {

    public static void GetRssItems(string rssFeed, Action < IEnumerable < RssItem >> onGetRssItemsCompleted = null, Action < string > onError = null, Action onFinally = null) {
        var request = HttpWebRequest.Create(rssFeed);
        request.Method = "GET";

        request.BeginGetResponse((result) = >{
            try {
                HttpWebRequest httpWebRequest = (HttpWebRequest) result.AsyncState;
                WebResponse webResponse = httpWebRequest.EndGetResponse(result);
                using(Stream stream = webResponse.GetResponseStream()) {
                    using(StreamReader reader = new StreamReader(stream)) {
                        string content = reader.ReadToEnd();
                        List < RssItem > rssItems = new List < RssItem > ();
                        SyndicationFeed feeds = new SyndicationFeed();
                        feeds.Load(content);
                        foreach(SyndicationItem f in feeds.Items) {
                            RssItem rssItem = new RssItem(f.Title.Text, f.Summary.Text, f.PublishedDate.ToString(), f.Links[0].Uri.AbsoluteUri);
                            rssItems.Add(rssItem);
                        }
                        if (onGetRssItemsCompleted != null) {
                            onGetRssItemsCompleted(rssItems);
                        }
                    }

                }
            } catch(WebException webEx) {
                string exceptionInfo = "";
                switch (webEx.Status) {
                case WebExceptionStatus.ConnectFailure:
                    exceptionInfo = "ConnectFailure:远程服务器连接失败。";
                    break;
                case WebExceptionStatus.MessageLengthLimitExceeded:
                    exceptionInfo = "MessageLengthLimitExceeded:网路请求的消息长度受到限制。";
                    break;
                case WebExceptionStatus.Pending:
                    exceptionInfo = "Pending:内部异步请求挂起。";
                    break;
                case WebExceptionStatus.RequestCanceled:
                    exceptionInfo = "RequestCanceled:该请求将被取消。";
                    break;
                case WebExceptionStatus.SendFailure:
                    exceptionInfo = "SendFailure:发送失败,未能将完整请求发送到远程服务器。";
                    break;
                case WebExceptionStatus.UnknownError:
                    exceptionInfo = "UnknownError:未知错误。";
                    break;
                case WebExceptionStatus.Success:
                    exceptionInfo = "Success:请求成功。";
                    break;
                default:
                    exceptionInfo = "未知网络异常。";
                    break;
                }
                if (onError != null) {
                    onError(exceptionInfo);
                }
            } catch(Exception e) {
                if (onError != null) {
                    onError("异常:" + e.Message);
                }
            } finally {
                if (onFinally != null) {
                    onFinally();
                }
            }
        },
        request);

    }
}

网友评论

无吐槽,不精彩,期待您的参与。

吐槽几句

"(必填)"

"(选填)"

"(必填,不公开)"

(必填,最多100个字符。)

先说这么多吧