Skip to content

Instantly share code, notes, and snippets.

@rudyryk
Last active August 22, 2018 13:51
Show Gist options
  • Save rudyryk/09c747fe9e144bd963ae to your computer and use it in GitHub Desktop.
Save rudyryk/09c747fe9e144bd963ae to your computer and use it in GitHub Desktop.
C# — Simple rounded avatar class example for Xamarin.Forms
//
// Avatar.cs
// Created by Alexey Kinev on 26 Feb 2015.
//
// Licensed under The MIT License (MIT)
// http://opensource.org/licenses/MIT
//
// Simple rounded avatar class example for Xamarin.Forms.
//
using System;
using Xamarin.Forms;
using ImageCircle.Forms.Plugin.Abstractions;
namespace Project
{
public class Avatar : AbsoluteLayout
{
/// <summary>
/// The image URI property.
/// </summary>
public static readonly BindableProperty ImageUriProperty =
BindableProperty.Create("ImageUri", typeof(String), typeof(Avatar), "");
/// <summary>
/// The circle background color property.
/// </summary>
public static readonly BindableProperty CircleBackgroundColorProperty =
BindableProperty.Create("CircleBackgroundColor", typeof(Color), typeof(Avatar), Color.Default);
/// <summary>
/// The name property.
/// </summary>
public static readonly BindableProperty NameProperty =
BindableProperty.Create("Name", typeof(String), typeof(Avatar), "");
/// <summary>
/// Gets or sets the image URI.
/// </summary>
public string ImageUri
{
get { return (string)GetValue(ImageUriProperty); }
set { SetValue(ImageUriProperty, value); }
}
/// <summary>
/// Gets or sets the color of the circle background.
/// </summary>
/// <value>The color of the circle background.</value>
public Color CircleBackgroundColor
{
get { return (Color)GetValue(CircleBackgroundColorProperty); }
set { SetValue(CircleBackgroundColorProperty, value); }
}
/// <summary>
/// The default avatar image.
/// </summary>
static ImageSource GetDefaultImageSource()
{
if (defaultImage == null)
{
defaultImage = ImageSource.FromFile("default-avatar.png");
}
return defaultImage;
}
static ImageSource defaultImage;
/// <summary>
/// Gets or sets the name.
/// </summary>
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
/// <summary>
/// The circle.
/// </summary>
readonly RoundedBox circle;
/// <summary>
/// The image.
/// </summary>
readonly CircleImage image;
/// <summary>
/// The name label.
/// </summary>
readonly Label nameLabel;
/// <summary>
/// Initializes a new instance of the <see cref="Project.Avatar"/> class.
/// </summary>
public Avatar()
{
// Circle
circle = new Circle {
BindingContext = this
};
circle.SetBinding(BackgroundColorProperty, "CircleBackgroundColor");
circle.SetBinding(IsVisibleProperty, "ImageUri",
converter: IsEmptyConverter.Instance); // show circle if image IS empty
Children.Add(circle, new Rectangle(0.0, 0.0, 1.0, 1.0), AbsoluteLayoutFlags.All);
// Name
nameLabel = new Label {
TextColor = Theme.LightTextColor,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center,
FontSize = Theme.LargeFontSize,
BindingContext = this
};
nameLabel.SetBinding(Label.TextProperty, "Name",
converter: NameToInitialsConverter.Instance);
Children.Add(nameLabel, new Rectangle(0, 0, 1.0, 1.0), AbsoluteLayoutFlags.All);
// Image
image = new CircleImage {
BorderThickness = 0,
HorizontalOptions = LayoutOptions.Center,
Aspect = Aspect.AspectFill,
BackgroundColor = Color.Transparent,
BindingContext = this
};
// We need intermediate cache layer here
image.SetBinding(Image.SourceProperty, "ImageUri",
converter: ImageSourceCachedConverter.Instance);
// WOW!!! Pre-loading image to static data makes ListView scrolling become smooth!!!
// image.Source = GetDefaultImageSource();
// This works not as expected! Image is loaded, but seems like not cached on
// per-name basis and is reloaded every time or something like that.
// image.Source = ImageSource.FromFile("default-avatar.png");
Children.Add(image, new Rectangle(0, 0, 1.0, 1.0), AbsoluteLayoutFlags.All);
}
}
}
//
// ImageSourceCachedConverter.cs
// Created by Alexey Kinev on 26 Feb 2015.
//
// Licensed under The MIT License (MIT)
// http://opensource.org/licenses/MIT
//
// Xamarin.Forms value converter for binding image sources by remote
// URIs with basic in-memory cache.
//
using System;
using System.Collections.Concurrent;
using Xamarin.Forms;
namespace Project
{
using CacheDictionary = ConcurrentDictionary<string, ImageSource>;
public class ImageSourceCachedConverter : IValueConverter
{
/// <summary>
/// The instance of converter.
/// </summary>
public static readonly ImageSourceCachedConverter Instance =
new ImageSourceCachedConverter();
/// <summary>
/// The cache.
/// </summary>
CacheDictionary cache;
/// <summary>
/// Initializes a new instance of the <see cref="Project.ImageSourceCachedConverter"/> class.
/// </summary>
ImageSourceCachedConverter()
{
cache = new CacheDictionary();
}
#region IValueConverter implementation
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var source = value as string;
if (!String.IsNullOrEmpty(source))
{
return cache.GetOrAdd(source, (imageUri) =>
ImageSource.FromUri(new Uri(imageUri)));
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment