2015-08-16 55 views
1

我有問題。我編程的Windows Phone 8.0應用程序,我沒有看到我的應用程序中的任何圖片。大概錯誤是在正則表達式,因爲在調試regImg沒有任何比賽我在我的RSS中看不到任何圖片

類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; 
    } 
+0

1.代替'[\ S \ t \ r \ n] *'用'\ S *' - 2.使用'\ /?'而不是'/?';)。 –

+0

3.如何使用簡單的正則表達式像'] *?src \ s * = \ s * [「''](? [^」''] *)[「''] [^>] *> '? )。 –

+0

任何解決方案不起作用:( – Remi

回答

0

確定。我在第二個項目中以不同的方式寫了一點(昨天我寫了一個新項目)。雖然問題是相同的,但它不會加載圖片。功能HasImage總是返回false(雖然當圖片存在),variabe圖片爲空(變量其餘的都是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)); 
     } 
    } 
} 
+0

可以請你在這裏提供一個圖像的樣本鏈接??請從調試器複製。 – ssakash

+0

[鏈接] http://wrzucaj.net/image/1O5 [/鏈接]它的屏幕從調試器 – Remi

+0

匹配匹配=新的正則表達式(「src = "(。*)」); 嘗試這一個..... – ssakash

相關問題