我在RSS里没有看到任何图片

本文关键字:任何图 RSS 我在 | 更新日期: 2023-09-27 18:08:12

我有问题。我编程Windows Phone 8.0应用程序,我没有看到任何图片在我的应用程序。可能错误是在正则表达式,因为在调试机制没有任何匹配

类MainPage.xaml.cs

string strURL = "https://news.google.com/news?   cf=all&ned=pl_pl&hl=pl&topic=b&output=rss"; // URL of RssFeeds.

和类ImageFromRssText.cs

public class ImageFromRssText : IValueConverter
{
    //  Get images from each SyndicationItem. 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return null;
        List<ImageItem> listUri = GetHtmlImageUrlList(value.ToString());
        return listUri;
    }
    /// <summary> 
    /// Get the URL of the all pictures from the HTML. 
    /// </summary> 
    /// <param name="sHtmlText">HTML code</param> 
    /// <returns>URL list of the all pictures</returns> 
    public static List<ImageItem> GetHtmlImageUrlList(string sHtmlText)
    {
        // The definition of a regular expression to match img tag. 
        Regex regImg = new Regex(@"<img'b[^<>]*?'bsrc['s't'r'n]*=['s't'r'n]*[""']?['s't'r'n]*(?<imgUrl>[^'s't'r'n""'<>]*)[^<>]*?/?['s't'r'n]*>", RegexOptions.IgnoreCase);
        // The search for a matching string.
        MatchCollection matches = regImg.Matches(sHtmlText);
        int i = 0;
        List<ImageItem> imgUrlList = new List<ImageItem>();


        // Get a list of matches
        foreach (Match match in matches)
        {
            imgUrlList.Add(new ImageItem("img" + i, match.Groups["imgUrl"].Value));
            i++;
        }
        return imgUrlList;
    }

我在RSS里没有看到任何图片

好的。我在第二个项目中以一种不同的方式写了一点(昨天我写了一个新的项目)。虽然问题是一样的,但它不加载图片。函数HasImage总是返回false(尽管当图片存在时)并且变量Image为null(其他变量是OK的)

 public class FeedItemViewModel : System.ComponentModel.INotifyPropertyChanged
{
    // Declaration - Title, Image, Lead, Url
  private string _title;
    public string Title
    {
        get
        {
            return _title;
        }
        set
        {
            _title = value;
            OnPropertyChanged("Title");
        }
    }
    // All from RSS (Image and lead)
    private string _lead;
    public string Lead
    {
        get
        {
            return _lead;
        }
        set
        {
            _lead = value;
            // Load picture
            try
            {
                if (TryParseImageUrl(_lead, out _imageUrl))
                    _imageUrl = _imageUrl.Replace("//", "http://");
            }
            catch { }
            OnPropertyChanged("Lead");
        }
    }
    private string _imageUrl;
    public Uri Image
    {
        get
        {
            if (HasImage)
                return new Uri(_imageUrl, UriKind.RelativeOrAbsolute);
            return null;
        }
    }
    // Check if picture exists
    public bool HasImage
    {
        get
        {
            return (!string.IsNullOrEmpty(_imageUrl) && Uri.IsWellFormedUriString(_imageUrl, UriKind.RelativeOrAbsolute));
        }
    }
    // Download url news
    private string _url;
    public string Url
    {
        get
        {
            return _url;
        }
        set
        {
            _url = value;
            OnPropertyChanged("Url");
        }
    }
    public void OnOpenUrl()
    {
        var wb = new Microsoft.Phone.Tasks.WebBrowserTask();
        wb.Uri = new Uri(_url);
        wb.Show();
    }

    // 3 method parse image
    private static bool TryParseImageUrl(string description, out string result)
    {
        string str = ParseAnyImageInTheDescription(description);
        result = str;
        return (!string.IsNullOrEmpty(str) && Uri.IsWellFormedUriString(str, UriKind.RelativeOrAbsolute));
    }
    private static string ParseAnyImageInTheDescription(string item)
    {
        if (item == null) { return null; }
        return ParseImageUrlFromHtml(item);
    }
    private static string ParseImageUrlFromHtml(string html)
    {
        Match match = new Regex("src=(?:'''"|''')?(?<imgSrc>[^>]*[^/].(?:jpg|png|jpeg))(?:'''"|''')?").Match(html);
        if ((match.Success && (match.Groups.Count > 1)) && (match.Groups[1].Captures.Count > 0))
        {
            return match.Groups[1].Captures[0].Value;
        }
        return null;
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}