0

I'm trying to make this switch renderer work but visual studio doens't recognize CustomSwitch and compilation fails with message "The type 'local:CustomSwitch' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. (MasterDetailPageNavigation)"

I have CustomSwitch.cs in my project folder:

using Xamarin.Forms;

public class CustomSwitch : Switch
{
    public static readonly BindableProperty SwitchOffColorProperty =
      BindableProperty.Create(nameof(SwitchOffColor),
          typeof(Color), typeof(CustomSwitch),
          Color.Default);

    public Color SwitchOffColor
    {
        get { return (Color)GetValue(SwitchOffColorProperty); }
        set { SetValue(SwitchOffColorProperty, value); }
    }

    public static readonly BindableProperty SwitchOnColorProperty =
      BindableProperty.Create(nameof(SwitchOnColor),
          typeof(Color), typeof(CustomSwitch),
          Color.Default);

    public Color SwitchOnColor
    {
        get { return (Color)GetValue(SwitchOnColorProperty); }
        set { SetValue(SwitchOnColorProperty, value); }
    }

    public static readonly BindableProperty SwitchThumbColorProperty =
      BindableProperty.Create(nameof(SwitchThumbColor),
          typeof(Color), typeof(CustomSwitch),
          Color.Default);

    public Color SwitchThumbColor
    {
        get { return (Color)GetValue(SwitchThumbColorProperty); }
        set { SetValue(SwitchThumbColorProperty, value); }
    }

    public static readonly BindableProperty SwitchThumbImageProperty =
      BindableProperty.Create(nameof(SwitchThumbImage),
          typeof(string),
          typeof(CustomSwitch),
          string.Empty);

    public string SwitchThumbImage
    {
        get { return (string)GetValue(SwitchThumbImageProperty); }
        set { SetValue(SwitchThumbImageProperty, value); }
    }
}

In project.droid folder I have CustomSwitchRenderer.cs:

using System;
using Android.Graphics;
using Android.Widget;
using MasterDetailPageNavigation.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace MasterDetailPageNavigation.Droid
{
    [Obsolete]
    public class CustomSwitchRenderer : SwitchRenderer
    {
        private CustomSwitch view;
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null || e.NewElement == null)
                return;
            view = (CustomSwitch)Element;
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean)
            {
                if (this.Control != null)
                {
                    if (this.Control.Checked)
                    {
                        this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
                    }
                    else
                    {
                        this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
                    }
                    this.Control.CheckedChange += this.OnCheckedChange;
                    UpdateSwitchThumbImage(view);
                }
                //Control.TrackDrawable.SetColorFilter(view.SwitchBGColor.ToAndroid(), PorterDuff.Mode.Multiply);  
            }
        }

        private void UpdateSwitchThumbImage(CustomSwitch view)
        {
            if (!string.IsNullOrEmpty(view.SwitchThumbImage))
            {
                view.SwitchThumbImage = view.SwitchThumbImage.Replace(".jpg", "").Replace(".png", "");
                int imgid = (int)typeof(Resource.Drawable).GetField(view.SwitchThumbImage).GetValue(null);
                Control.SetThumbResource(Resource.Drawable.icon);
            }
            else
            {
                Control.ThumbDrawable.SetColorFilter(view.SwitchThumbColor.ToAndroid(), PorterDuff.Mode.Multiply);
                // Control.SetTrackResource(Resource.Drawable.track);  
            }
        }

        private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            if (this.Control.Checked)
            {
                this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
            else
            {
                this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
        }
        protected override void Dispose(bool disposing)
        {
            this.Control.CheckedChange -= this.OnCheckedChange;
            base.Dispose(disposing);
        }
    }
}

And this is the relevant part of my xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:behavior="clr-namespace:MasterDetailPageNavigation.XAML"
             xmlns:local="clr-namespace:MasterDetailPageNavigation"
             x:Class="MasterDetailPageNavigation.XAML.CompletaCadastroProf"
             BackgroundImageSource="background">
    <ContentPage.Content>
       <StackLayout>

        <local:CustomSwitch SwitchOnColor="Red" Grid.Column="0" Grid.Row="0" />

 ...
 ...

May be I forgot to add some reference, are you guys seeing what I'm doing wrong?

hvaughan3
  • 10,955
  • 5
  • 56
  • 76
Sandro Benevides
  • 581
  • 2
  • 14
  • 1
    `CustomSwitch` does not appear to use any namespace, so it does not match the namespace your declare with `xmlns:local` – Jason Mar 17 '20 at 23:03

2 Answers2

0

First of all,If you do not put your CustomSwitch in the root path of this project like this screenshot, enter image description here

Please change another Prefix I changed myswitch my code like following format.

  <myswitch:CustomSwitch SwitchOffColor="Green"  SwitchOnColor="Red"  />

If the VS have auto-association namespace,please click the Alt+Enter, you will get the following sceenshot. Click the add xmlns xxxxxxxx enter image description here

If the VS do not No auto-association namespace, you can right click your PCL item, select the Properties. enter image description here

Then copy the value of default namespace. enter image description here

Open your relevant xaml add this xmlns:myswitch="clr-namespace:xxxxxxx" :

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:myswitch="clr-namespace:SqliteFormsMVVM"
             mc:Ignorable="d"
             x:Class="SqliteFormsMVVM.MainPage">

    <StackLayout>
        <myswitch:CustomSwitch SwitchOffColor="Green"  SwitchOnColor="Red"  />


    </StackLayout>

</ContentPage>

If you have done above settings, this issue is still existed, please close your VS, open your project folder, delete all of bin and obj folders(xxx,xxx.Android,xxx.iOS folders), in the end, rebuild your project.

Leon
  • 8,404
  • 2
  • 9
  • 52
0

I missed to give a namespace for CustomSwitch.cs, just added

namespace MasterDetailPageNavigation.XAML
{ 
...
}

and everything works now.

Sandro Benevides
  • 581
  • 2
  • 14