C# 按钮的Xamarin自定义渲染器(iOS)
我已经为iOS和Android声明了一个自定义渲染器-工作正常 自定义渲染器主要设置背景颜色和文本颜色 设置文本颜色在启用和禁用状态下效果很好,但在不同状态下设置按钮的背景色时遇到问题 我还没有找到任何有关Xamarin自定义渲染器的文档,Xamarin的一个已知错误是,我无法在Visual Studio中为iOS类提供任何intellisense,到目前为止,我已经使用了我能找到的有关该主题的资源C# 按钮的Xamarin自定义渲染器(iOS),c#,button,xamarin.ios,xamarin.forms,custom-renderer,C#,Button,Xamarin.ios,Xamarin.forms,Custom Renderer,我已经为iOS和Android声明了一个自定义渲染器-工作正常 自定义渲染器主要设置背景颜色和文本颜色 设置文本颜色在启用和禁用状态下效果很好,但在不同状态下设置按钮的背景色时遇到问题 我还没有找到任何有关Xamarin自定义渲染器的文档,Xamarin的一个已知错误是,我无法在Visual Studio中为iOS类提供任何intellisense,到目前为止,我已经使用了我能找到的有关该主题的资源 public class MyButtonRenderer : ButtonRender
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.BackgroundColor= UIColor.FromRGB(235, 115, 17);
Control.SetTitleColor(UIColor.FromRGB(255, 255, 255),UIControlState.Normal);
Control.SetTitleColor(UIColor.FromRGB(0, 0, 0),UIControlState.Disabled);
}
}
}
如果按钮uicontrol状态被禁用,我希望能够将背景颜色更改为我设置的颜色以外的颜色
在此图像中,按钮使用自定义渲染器。顶部按钮禁用,底部按钮启用。正如你可能猜到的,我想把禁用的按钮变成灰色
我相信这一定很简单,但是缺乏文档和智能感知问题阻碍了我的努力 您可以覆盖OnElementPropertyChanged以跟踪属性更改
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
....
UpdateBackground();
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
UpdateBackground();
}
void UpdateBackground()
{
if (Control == null || Element == null)
return;
if (Element.IsEnabled)
Control.BackgroundColor = ..;
else
Control.BackgroundColor = ..;
}