【问题标题】:how do i change color on the switch in both android and ios?如何在android和ios中更改开关的颜色?
【发布时间】:2019-06-24 10:43:53
【问题描述】:

我需要能够在xamarin.forms 中更改androidios 上的开关颜色,因为一个是绿色,另一个是黑色,但应用程序的主要颜色是黄色#FED000,我该怎么办?

【问题讨论】:

  • 我已经解决了android,我已经改变了颜色主题,但iOS没有解决
  • iOS 上的OnColor 是什么意思?你没有实现哪个部分?

标签: c# android ios xamarin.forms


【解决方案1】:

您可以创建自定义渲染器:

在您的表单解决方案中:

public class CustomSwitch : Switch
{
}

然后,在您的 iOS 解决方案中:

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]

namespace yourNameSpace
{
    public class CustomSwitchRenderer : SwitchRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Switch> e)
        {
            base.OnElementChanged (e);

            if (Control != null) 
            {
                 //change color
                Control.OnTintColor = UIColor.FromRGB (204, 153, 255);
            }
         }
    }
}

但是,如果您想更改应用程序中的所有开关,您可以简单地:

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    // switch
    UISwitch.Appearance.OnTintColor = UIColor.FromRGB(0,0,0);


    return base.FinishedLaunching (app, options);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 2023-03-15
    • 1970-01-01
    • 2012-06-30
    • 2021-06-26
    • 2016-08-11
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多