Problem creating a renderer for a custom Switch using XamarinForms

So, I'm working on a Android app project in which I need to create a Switch control with customized text for the toggled options. I've tried creating a custom text Switch using a custom Renderer, but have had an exception whenever I try using my custom control.

I've created a custom TextSwitch control:

using System;
using AutoLogSchedulingMobile.Resources;
using Xamarin.Forms;


namespace AutoLogSchedulingMobile.CustomControls
{
    public class TextSwitch : Switch
    {
        public string TextOn
        {
            get { return "OK"; }
        }

        public string TextOff
        {
            get { return "NÂO OK"; }
        }
    }
}

And for the renderer:

using System;
using Android.Content;
using Android.Widget;
using AutoLogSchedulingMobile.CustomControls;
using AutoLogSchedulingMobile.Droid.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Switch = Android.Widget.Switch;

[assembly: ExportRenderer(typeof(TextSwitch), typeof(TextSwitchRenderer))]
namespace AutoLogSchedulingMobile.Droid.Controls
{

    public class TextSwitchRenderer : ViewRenderer<TextSwitch, Switch>
    {
        public TextSwitchRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<TextSwitch> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null)
            {
                this.Element.Toggled -= ElementToggled;
                return;
            }

            if (this.Element == null)
            {
                return;
            }

            var switchControl = new Switch(Context)
            {
                TextOn = this.Element.TextOn,
                TextOff = this.Element.TextOff
            };

            switchControl.CheckedChange += ControlValueChanged;
            this.Element.Toggled += ElementToggled;

            this.SetNativeControl(switchControl);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.Control.CheckedChange -= this.ControlValueChanged;
                this.Element.Toggled -= ElementToggled;
            }

            base.Dispose(disposing);
        }

        private void ElementToggled(object sender, ToggledEventArgs e)
        {
            this.Control.Checked = this.Element.IsToggled;
        }

        private void ControlValueChanged(object sender, EventArgs e)
        {
            this.Element.IsToggled = this.Control.Checked;
        }
    }

}

I am however getting a System.Reflection.TargetInvocationException whenever I try using this custom Switch control in any xaml page in my app. I'm not sure where the mistake is or if there is another way to implement a custom Switch with text using Xamarin forms. I'm attaching a copy of the output in a text file. I'd be grateful for any pointers.

Answers

  • MNadeemMNadeem Member ✭✭

    @AugustV2 said:
    So, I'm working on a Android app project in which I need to create a Switch control with customized text for the toggled options. I've tried creating a custom text Switch using a custom Renderer, but have had an exception whenever I try using my custom control.

    I've created a custom TextSwitch control:

    using System;
    using AutoLogSchedulingMobile.Resources;
    using Xamarin.Forms;
    
    
    namespace AutoLogSchedulingMobile.CustomControls
    {
        public class TextSwitch : Switch
        {
            public string TextOn
            {
                get { return "OK"; }
            }
    
            public string TextOff
            {
                get { return "NÂO OK"; }
            }
        }
    }
    

    And for the renderer:

    using System;
    using Android.Content;
    using Android.Widget;
    using AutoLogSchedulingMobile.CustomControls;
    using AutoLogSchedulingMobile.Droid.Controls;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using Switch = Android.Widget.Switch;
    
    [assembly: ExportRenderer(typeof(TextSwitch), typeof(TextSwitchRenderer))]
    namespace AutoLogSchedulingMobile.Droid.Controls
    {
    
        public class TextSwitchRenderer : ViewRenderer<TextSwitch, Switch>
        {
            public TextSwitchRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<TextSwitch> e)
            {
                base.OnElementChanged(e);
                if (e.OldElement != null)
                {
                    this.Element.Toggled -= ElementToggled;
                    return;
                }
    
                if (this.Element == null)
                {
                    return;
                }
    
                var switchControl = new Switch(Context)
                {
                    TextOn = this.Element.TextOn,
                    TextOff = this.Element.TextOff
                };
    
                switchControl.CheckedChange += ControlValueChanged;
                this.Element.Toggled += ElementToggled;
    
                this.SetNativeControl(switchControl);
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    this.Control.CheckedChange -= this.ControlValueChanged;
                    this.Element.Toggled -= ElementToggled;
                }
    
                base.Dispose(disposing);
            }
    
            private void ElementToggled(object sender, ToggledEventArgs e)
            {
                this.Control.Checked = this.Element.IsToggled;
            }
    
            private void ControlValueChanged(object sender, EventArgs e)
            {
                this.Element.IsToggled = this.Control.Checked;
            }
        }
    
    }
    

    I am however getting a System.Reflection.TargetInvocationException whenever I try using this custom Switch control in any xaml page in my app. I'm not sure where the mistake is or if there is another way to implement a custom Switch with text using Xamarin forms. I'm attaching a copy of the output in a text file. I'd be grateful for any pointers.

  • MNadeemMNadeem Member ✭✭

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

    your element or control name is not right please check it and try again. after it may be this will problem with your renderer

Sign In or Register to comment.