我的代码使用的标准开关如下:
<Switch HorizontalOptions="End" IsToggled="{Binding PbSwitch}" />
显示时,它显示为开启和关闭:
有没有办法自定义这个,所以我可以在iOS和Android中将开关的背景颜色设置为不同的颜色。我认为这需要一个自定义渲染器,但我不知道该怎么做。
提出的解决方案存在问题:
答案 0 :(得分:3)
如果我明白你想要什么,你可能需要自定义渲染器才能达到这个效果。
首先,在PCL中使用颜色BindableProperty定义一个CustomSwitch(用它可以设置值并在不同平台上使用)
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); }
}
}
[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace FormsApp.Android
{
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);
}
}
}
[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace FormsApp2.iOS
{
class CustomSwitchRenderer : SwitchRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || e.NewElement == null) return;
CustomSwitch s = Element as CustomSwitch;
UISwitch sw = new UISwitch();
sw.ThumbTintColor = s.SwitchThumbColor.ToUIColor();
sw.OnTintColor = s.SwitchOnColor.ToUIColor();
SetNativeControl(sw);
}
}
}
<local:CustomSwitch Margin="50" SwitchOnColor="Red"/>
不要忘记在Xmal中添加xmlns(名称空间)
xmlns:local = "clr-namespace:YouAppName"
答案 1 :(得分:0)
我对IOS Xamarin Forms也有类似的问题:
Change Switch color in IOS Xamarin Forms
我使用自定义渲染解决了下一个代码:
[assembly: ExportRenderer(typeof(MySwitch), typeof(CustomSwitchRenderer))]
namespace CheckMyBus.App.ios.Renderers
{
public class CustomSwitchRenderer: SwitchRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
Element.Toggled += ElementToggled;
base.OnElementChanged(e);
if (Control != null)
{
UpdateUiSwitchColor();
}
}
private void ElementToggled(object sender, ToggledEventArgs e)
{
UpdateUiSwitchColor();
}
private void UpdateUiSwitchColor()
{
var temp = Element as Switch;
if (temp.IsToggled)
{
Control.ThumbTintColor = Color.FromHex(ColorConstants.BlueHex).ToUIColor();
Control.OnTintColor = Color.FromHex(ColorConstants.OblueLightHex).ToUIColor();
}
else
{
Control.ThumbTintColor = Color.FromHex(ColorConstants.GrayHex).ToUIColor();
}
}
}
}
并在Android Xamarin Forms项目中使用
完成<item name="colorAccent">#6495ED</item>
在MainTheme.Base样式的style.xml中。
所以这看起来比创建自定义开关更简单,你只需要这样的东西,CustomSwitchRenderer
就可以了:
public class MySwitch: Switch
{
}
并在XAML中:
<myControls:MySwitch x:Name="" />
答案 2 :(得分:0)
在Xamarin.Form共享应用程序(我的版本为v.4.6)中,可以简单地将颜色添加到OnColor(切换为真实背景)和ThumbColor(圆形切换)属性中。此值接受命名的颜色,rgb,rgba等。
<Switch IsToggled="True" OnColor="Red" ThumbColor="#d9d9d9" />