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

Custom Renderer not showing Value on initialisaztion

$
0
0

Hello there

I'm currently working on an app which shows tasks to the user. The tasks have a progressbar which can be changed by the user.
My idea was to use a slider with a custom renderer, but it's not working. I'm quite new to Xamarin Forms and don't know much about custom renderers.
I followed this tutorial: theconfuzedsourcecode.wordpress.com/2017/04/06/advanced-decorating-of-xamarin-forms-slider-for-android/ (I'm not allowed to post links yet...)

It's working fine when I change the value but when it's binded to a value from a observable collection in a listview it's not showing anything on initialisation. The value is set correctly. It's just not displayed correctly.

Here is my code for the Android-Renderer (which is not displaying on initialisation):

public class ProgressSliderRenderer : SliderRenderer
    {

        public ProgressSliderRenderer(Context c) : base(c)
        {
            SetupDrawable();  
        }



        protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
        {
            base.OnElementChanged(e);
            SetupDrawable();
        }

        private void SetupDrawable()
        {
            if (Control != null)
            {
                // progress
                var progress = new PaintDrawable(Android.Graphics.Color.Rgb(0, 204, 255));
                progress.SetCornerRadius((int)DpToPixels(this.Context, 0));
                progress.SetIntrinsicHeight((int)DpToPixels(this.Context, 40));
                var progressClip = new ClipDrawable(progress, GravityFlags.Left, ClipDrawableOrientation.Horizontal);


                // background
                PaintDrawable background = new PaintDrawable(Android.Graphics.Color.Black);
                background.SetCornerRadius((int)DpToPixels(this.Context, 0));
                background.SetIntrinsicHeight((int)DpToPixels(this.Context, 40));

                // retrieve LayerDrawable reference of the SeekBar control
                LayerDrawable layeredDrawableReference = (LayerDrawable)Control.ProgressDrawable;

                // apply our custom drawable objects to the 
                // given sub-views through their IDs

                layeredDrawableReference.SetDrawableByLayerId(Android.Resource.Id.Background, background);
                layeredDrawableReference.SetDrawableByLayerId(Android.Resource.Id.Progress, progressClip);


                // Hide thumb to make it look cool lol
                Control.SetThumb(new ColorDrawable(Android.Graphics.Color.Transparent));

            }
        }

        /// <summary>
        /// Device Independent Pixels to Actual Pixles conversion
        /// </summary>
        /// <param name="context"></param>
        /// <param name="valueInDp"></param>
        /// <returns></returns>
        public static float DpToPixels(Context context, float valueInDp)
        {
            DisplayMetrics metrics = context.Resources.DisplayMetrics;
            return TypedValue.ApplyDimension(ComplexUnitType.Dip, valueInDp, metrics);
        }

    }

Thanks for any answers.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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