更改IOS Xamarin表单中的开关颜色 [英] Change Switch color in IOS Xamarin Forms
本文介绍了更改IOS Xamarin表单中的开关颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
切换IOS Xamarin Forms时如何更改切换按钮的颜色?自定义渲染将只设置一次颜色:
How to change the color of the switch button when toggling for IOS Xamarin Forms? Custom render will set the color just once:
,切换开关时不会更改.同样,在AppDelegate.cs中设置值仅会执行一次.我需要这个:
and it won't be changed when the Switch is toggled. Same goes and for setting values in AppDelegate.cs it will be done only once. I need this:
在IOS中,只能使用:
In IOS its only possible using:
UISwitch.Appearance.ThumbTintColor = UIColor.Brown;
UISwitch.Appearance.OnTintColor = UIColor.Red;
但是,如果我的开关在PCL项目中,并且Xamarin Forms用于Xamarin Forms,我将如何访问该字段.
but how I can access that field if my switch is in PCL project, and its used for Xamarin Forms.
推荐答案
我用以下方法解决了这个问题:
I solved this with:
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();
}
}
}
因此,无论何时执行Toggle,我都会激活事件:
So, I activated event whenever Toggle is executed:
Element.Toggled += ElementToggled;
这篇关于更改IOS Xamarin表单中的开关颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文