Affichage des articles dont le libellé est Converter. Afficher tous les articles
Affichage des articles dont le libellé est Converter. Afficher tous les articles

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.

12/07/2013

[Converter] BoolToVisibility

Allez c'est sans conteste, le converter dont vous vous servirez le plus. Convertir un Boolean en Visibility

Bon je pense qu'il n'y a pas besoin d'explication supplémentaire sur l'intérêt de ce converter donc :

public class BoolToVisibilityConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Vérification de la valeur
            if (value == null)
            {
                return null;
            }
            else
            {
                var myBoolean = (bool)value;

                // Vérification du paramètre
                if (parameter != null)
                {
                    // Autant de paramètre qu'on veut
                    switch (parameter.ToString())
                    {
                        case "invert":
                            return myBoolean ? Visibility.Collapsed : Visibility.Visible;
                        default:
                            return myBoolean ? Visibility.Visible : Visibility.Collapsed;
                    }
                }
                else
                {
                    return myBoolean ? Visibility.Visible : Visibility.Collapsed;
                }
            }
        }

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

Pour en savoir plus sur les converter c'est par ici

Une notion abordée ici qui n'est pas abordée dans mon précédent article est le ConverterParameter qui sert tout simplement à passer un paramètre à mon converter. Dans ma classe, le paramètre invert permet d'inverser le résultat.


    
        
    


Il est à noter qu'en WPF le framework embarque directement le converter donc, il reste plus qu'à l'instancier dans notre code côté XAML :


      

A Lundi pour une nouvelle vidéo.

05/07/2013

[Converter] ImagePicker

On inaugure aujourd'hui la catégorie des Converter en démarrant avec un sélecteur d'image.

Pour finir cette semaine tranquillement, je vous propose un sélecteur d'image :

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

namespace MyApp.WP8.View.Converter
{
    public class ImagePicker : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string matchString = Regex.Match(value.ToString(), @"(?<=<img.*?src=\"")[^\""]*(?=\"".*?((/>)|(>.*</img)))").Value;
            if (matchString == "")
            {
                return "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPgjvN03Mt9fpkGDo4AnQtOImddidJGYXbT18Yz7EyjBpt2NNrxZ1IvJXnCMNQEY-lNftZb66KYRznXWTCZSyH5HAkiRtD-JrMfbRaZQUzWUUz4PqOQlvlVFgu2zjV5YYlM3M0FMkyRUMN/s220/AvatarFB.png";
            }
            else
            {
                return matchString;
            }
        }

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

Très utile notamment pour les flux RSS, il va nous permettre de renvoyer une image par défaut si notre item ne contient pas d'image.

Bon WE. A lundi pour un nouveau Tuto vidéo.