05/08/2013

[WindowsPhone] TextPicker

Bonjour à toutes et à tous,
Je vous propose aujourd'hui un Converter pour récupérer uniquement le texte de votre flux rss. C'est par ici...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Data;

namespace SampleRSSReader.View.Converter
{
    public class TextPicker : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) 
            {
                return null;
            }

            string fixedString = "";

            // Supprime les retours à la ligne.
            fixedString = fixedString.Replace("\r", "").Replace("\n", "");

            // Supprime le balises. 
            fixedString = Regex.Replace(value.ToString(), "<[^>]+>", string.Empty);

            // Remplace les caractères encodés.
            fixedString = HttpUtility.HtmlDecode(fixedString);

            // Supprime les formats DateTime avec une heure non définie
            fixedString = fixedString.Replace("00:00:00", "");

            strLength = fixedString.ToString().Length;

            if (strLength == 0)
            {
                return null;
            }

            // Permet de limiter la longueur du texte.
            int maxLength = 4096;
            int strLength = 0;
            

            else if (strLength >= maxLength)
            {
                fixedString = fixedString.Substring(0, maxLength);
                fixedString = fixedString.Substring(0, fixedString.LastIndexOf(" "));
                fixedString += "...";
            }

            return fixedString;
        
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Voilà, on se retrouve en fin de semaine pour un nouvel article.

Aucun commentaire:

Enregistrer un commentaire