static public bool HandleException(Exception exception, TextBox outputField, MainPage rootPage)
        {
            SyndicationErrorStatus status = SyndicationError.GetStatus(exception.HResult);

            if (status != SyndicationErrorStatus.Unknown)
            {
                outputField.Text += "The response content is not valid. " +
                                    "Please make sure to use a URI that points to an Atom feed.\r\n";
            }
            else
            {
                WebErrorStatus webError = WebError.GetStatus(exception.HResult);

                if (webError == WebErrorStatus.Unauthorized)
                {
                    outputField.Text += "Incorrect username or password.\r\n";
                }
                else if (webError == WebErrorStatus.Unknown)
                {
                    // Neither a syndication nor a web error.
                    return(false);
                }
            }

            rootPage.NotifyUser(exception.Message, NotifyType.ErrorMessage);

            return(true);
        }
        public async static Task <string> GetFeedTitleAsync()
        {
            string            response = String.Empty;
            SyndicationFeed   feed     = new SyndicationFeed();
            SyndicationClient client   = new SyndicationClient();

            try
            {
                feed = await client.RetrieveFeedAsync(
                    new Uri("http://www.apress.com/index.php/dailydeals/index/rss"));

                response = feed.GetXmlDocument(SyndicationFormat.Rss20).GetXml();
            }
            catch (Exception ex)
            {
                SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult);
                if (status == SyndicationErrorStatus.InvalidXml)
                {
                    response += "Invalid XML!";
                }

                if (status == SyndicationErrorStatus.Unknown)
                {
                    response = ex.Message;
                }
            }

            return(response);
        }
        private async void GetFeed_Click(object sender, RoutedEventArgs e)
        {
            outputField.Text = "";

            // By default 'FeedUri' is disabled and URI validation is not required. When enabling the text box
            // validating the URI is required since it was received from an untrusted source (user input).
            // The URI is validated by calling Uri.TryCreate() that will return 'false' for strings that are not valid URIs.
            // Note that when enabling the text box users may provide URIs to machines on the intrAnet that require
            // the "Home or Work Networking" capability.
            Uri uri;

            if (!Uri.TryCreate(FeedUri.Text.Trim(), UriKind.Absolute, out uri))
            {
                rootPage.NotifyUser("Error: Invalid URI.", NotifyType.ErrorMessage);
                return;
            }

            SyndicationClient client = new SyndicationClient();

            client.BypassCacheOnRetrieve = true;

            // Although most HTTP servers do not require User-Agent header, others will reject the request or return
            // a different response if this header is missing. Use SetRequestHeader() to add custom headers.
            client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

            rootPage.NotifyUser("Downloading feed...", NotifyType.StatusMessage);
            outputField.Text = "Downloading feed: " + uri.ToString() + "\r\n";

            try
            {
                currentFeed = await client.RetrieveFeedAsync(uri);

                rootPage.NotifyUser("Feed download complete.", NotifyType.StatusMessage);

                DisplayFeed();
            }
            catch (Exception ex)
            {
                SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult);
                if (status == SyndicationErrorStatus.InvalidXml)
                {
                    outputField.Text += "An invalid XML exception was thrown. " +
                                        "Please make sure to use a URI that points to a RSS or Atom feed.";
                }

                if (status == SyndicationErrorStatus.Unknown)
                {
                    WebErrorStatus webError = WebError.GetStatus(ex.HResult);

                    if (webError == WebErrorStatus.Unknown)
                    {
                        // Neither a syndication nor a web error. Rethrow.
                        throw;
                    }
                }

                rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
            }
        }
Пример #4
0
        public async Task InitializePlaybackList()
        {
            // string uri = "https://channel9.msdn.com/Feeds/RSS"; //"http://www.xamarinpodcast.com/rss";
            string uri = "http://www.xamarinpodcast.com/rss";

            Uri _feedUri;

            if (!Uri.TryCreate(uri, UriKind.Absolute, out _feedUri))
            {
                return;
            }

            var client = new SyndicationClient {
                BypassCacheOnRetrieve = true
            };

            try
            {
                var _currentFeed = await client.RetrieveFeedAsync(_feedUri);

                var title = _currentFeed.Title;

                foreach (var item in _currentFeed.Items)
                {
                    // Display title.
                    var displayTitle = item.Title?.Text ?? "(no title)";
                    if (item.Links.Count > 1)
                    {
                        var linkUri      = item.Links[1].Uri;
                        var artUriString = item.ElementExtensions.FirstOrDefault(e => e.NodeName == "image")?.AttributeExtensions.FirstOrDefault(a => a.Name == "href")?.Value;
                        if (string.IsNullOrEmpty(artUriString))
                        {
                            artUriString = item.ElementExtensions.LastOrDefault(e => e.NodeName == "thumbnail")?.AttributeExtensions.FirstOrDefault(a => a.Name == "url")?.Value;
                        }

                        Uri artUri = null;
                        if (artUriString != null)
                        {
                            artUri = new Uri(artUriString);
                        }

                        var media = new MediaModel(linkUri)
                        {
                            Title = displayTitle, ArtUri = artUri
                        };

                        playlistView.Items.Add(media);
                        playbackList.Items.Add(media.MediaPlaybackItem);
                    }
                }
            }
            catch (Exception ex)
            {
                SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult);
                if (status == SyndicationErrorStatus.InvalidXml)
                {
                    Debug.WriteLine("An invalid XML exception was thrown. " +
                                    "Please make sure to use a URI that points to a RSS or Atom feed.");
                }

                if (status == SyndicationErrorStatus.Unknown)
                {
                    WebErrorStatus webError = WebError.GetStatus(ex.HResult);

                    if (webError == WebErrorStatus.Unknown)
                    {
                        // Neither a syndication nor a web error. Rethrow.
                        throw;
                    }
                }
                Debug.WriteLine(ex.Message);
            }

            // Subscribe for changes
            playbackList.CurrentItemChanged += PlaybackList_CurrentItemChanged;

            // Loop
            playbackList.AutoRepeatEnabled = true;

            playlistView.SelectedIndex = -1;
        }