Lui*_*ini 3 c# xamarin.android xamarin xamarin.forms
我需要在禁用按钮时更改其文本颜色,我为iOS创建了一个自定义渲染器,为Android创建了一个自定义渲染器。iOS完美运行,因为android不会改变颜色,所以我也通过样式创建了触发器,也无法解决。
如何使Xamarin.Forms进行颜色交换?
Android渲染器:
[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
namespace xxxxxx.Droid.Renderers
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
Control.SetTextColor(Color.White.ToAndroid());
if (e != null)
{
e.NewElement.TextColor = Color.White;
e.OldElement.TextColor = Color.White;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的命令的CanExecute更改此状态更改,这将是应用的默认样式。
这些方法都不能解决
Lit*_*Dev 10
我使用VisualStateManager为每个按钮状态添加不同的样式:正常、聚焦、按下、禁用
<Style TargetType="Button">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource ColorAccent}" />
<Setter Property="TextColor" Value="{StaticResource ColorForegroundIcon}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource ColorAccentTransparent}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource ColorAccentTransparent}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{DynamicResource ColorBackgroundDisabled}" />
<Setter Property="TextColor" Value="{DynamicResource ColorForegroundFaded}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
有关详细信息和示例,请查看:了解 Xamarin Forms 中的视觉状态管理器
对于安德鲁瓦:
[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
namespace Forms.Droid.Renderers
{
public class MyButtonRenderer : ButtonRenderer
{
public MyButtonRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Button> args)
{
base.OnElementChanged(args);
if (Control != null) SetColors();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);
if (args.PropertyName == nameof(Button.IsEnabled)) SetColors();
}
private void SetColors()
{
Control.SetTextColor(Element.IsEnabled ? Element.TextColor.ToAndroid() : Android.Graphics.Color.Gray);
Control.SetBackgroundColor(Element.IsEnabled ? Element.BackgroundColor.ToAndroid() : Android.Graphics.Color.DarkGray);
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于iOS:
[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
namespace Forms.iOS.Renderers
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> args)
{
base.OnElementChanged(args);
if (Control != null) SetColors();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);
if (args.PropertyName == nameof(Button.IsEnabled)) SetColors();
}
private void SetColors()
{
Control.SetTitleColor(Element.IsEnabled ? Element.TextColor.ToUIColor() : UIColor.Gray, Element.IsEnabled ? UIControlState.Normal : UIControlState.Disabled);
Control.BackgroundColor = Element.IsEnabled ? Element.BackgroundColor.ToUIColor() : UIColor.DarkGray;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果CanExecute按预期工作,那么您的IsEnabled属性应该相应更新。你可以通过该方法监听这个属性值的变化OnElementPropertyChanged。
public class MyButtonRenderer : ButtonRenderer
{
....
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(Button.IsEnabled))
{
Element.TextColor = Element.IsEnabled ? Color.White : Color.Gray;
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5791 次 |
| 最近记录: |