Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Custom Control, setting property B from Property A

$
0
0

I'm trying to wire up a GravatarImage control that implements Image. What I'm trying to do is set the value of Source from the bindable Email property, and fall back to Source if there are issues getting the remote image.

If I don't make the Email property Bindable, I can do exactly what I want, but in making the email source Bindable, I just get a blank spot where the image should be, and a breakpoint in the set_Email never hits. Anyone able to see what I'm missing?

        Master.BindingContext = new MenuPageViewModel(_signOutCommand, OnAfterDeauthenticated)
        {
            Projects = _menuPageViewModelMapper.BuildListFrom(projects).ToObservableCollection(), 
            Email = App.CurrentUser.Email, 
            FullName = string.Format("{0} {1}", App.CurrentUser.FirstName, App.CurrentUser.LastName)
        };
    <ctrl:GravatarImage IsCircle="True" WidthRequest="50" HeightRequest="50" Email="{Binding Email}"  />
    public class GravatarImage : ExtendedImage
    {
        public static readonly BindableProperty EmailProperty = BindableProperty.Create<GravatarImage, string>(p => p.Email, string.Empty);
        private int _gravatarSize = 100;

        public string Email
        {
            get { return (string) GetValue(SourceProperty); }
            set
            {
                SetValue(EmailProperty, value);
                SetValue(SourceProperty, GetSourceValue(value));
            }
        }

        public int GravatarSize
        {
            get { return _gravatarSize; }
            set { _gravatarSize = value; }
        }

        private ImageSource GetSourceValue(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) return Source;

            // there will actually be a network connetivity check here
            var generatedImageUrl = Gravatar.GetImageUrl(value, GravatarSize);
            var remoteImage = new UriImageSource {Uri = generatedImageUrl};
            return remoteImage;
        }
    }

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>