gpt4 book ai didi

c# - 如何更改 iOS 和 Android 步进器的颜色?

转载 作者:太空狗 更新时间:2023-10-29 18:28:37 24 4
gpt4 key购买 nike

我的代码使用如下所示的步进器:

Stepper control

如何通过在 XAML 中设置新颜色,将步进器的 iOS 和 Android 版本的颜色从蓝色更改为红色?

最佳答案

这可以使用 Effects 来完成.

代码

我在这里创建了一个示例应用程序:https://github.com/brminnick/CustomStepper

在 XAML 中使用效果

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomStepper.StepperPage"
xmlns:local="clr-namespace:CustomStepper">

<ContentPage.Content>
<Stepper
HorizontalOptions="Center"
VerticalOptions="Center"
local:StepperColorEffect.Color="Red"/>
</ContentPage.Content>
</ContentPage>

步进颜色效果

using System.Linq;

using Xamarin.Forms;

namespace CustomStepper
{
public static class StepperColorEffect
{
public static readonly BindableProperty ColorProperty =
BindableProperty.CreateAttached(nameof(Color),
typeof(Color),
typeof(Stepper),
Color.Gray,
propertyChanged: OnStepperColorChanged);

public static Color GetColor(BindableObject view) => (Color)view.GetValue(ColorProperty);

public static void SetColor(BindableObject view, Color value) => view.SetValue(ColorProperty, value);

static void OnStepperColorChanged(BindableObject bindable, object oldValue, object newValue) => UpdateEffect(bindable);

static void UpdateEffect(BindableObject bindable)
{
var stepper = (Stepper)bindable:
RemoveEffect(stepper);
stepper.Effects.Add(new StepperColorRoutingEffect());
}

static void RemoveEffect(Stepper entry)
{
var effectToRemoveList = entry.Effects.OfType<StepperColorRoutingEffect>();

foreach (var entryReturnTypeEffect in effectToRemoveList)
entry.Effects.Remove(entryReturnTypeEffect);
}
}

class StepperColorRoutingEffect : RoutingEffect
{
public StepperColorRoutingEffect() : base("CustomStepper.StepperColorEffect")
{
}
}
}

iOS 平台效果

using UIKit;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using CustomStepper.iOS;

[assembly: ResolutionGroupName("CustomStepper")]
[assembly: ExportEffect(typeof(StepperColorEffect), nameof(StepperColorEffect))]
namespace CustomStepper.iOS
{
public class StepperColorEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Element is Stepper element && Control is UIStepper control)
{
control.TintColor = CustomStepper.StepperColorEffect.GetColor(element).ToUIColor();
control.SetIncrementImage(Control.GetIncrementImage(UIControlState.Normal), UIControlState.Normal);
control.SetDecrementImage(Control.GetDecrementImage(UIControlState.Normal), UIControlState.Normal);
}
}

protected override void OnDetached()
{
if (Element is Stepper element && Control is UIStepper control)
{
control.TintColor = UIColor.Blue;
control.SetIncrementImage(Control.GetIncrementImage(UIControlState.Normal), UIControlState.Normal);
control.SetDecrementImage(Control.GetDecrementImage(UIControlState.Normal), UIControlState.Normal);
}
}
}
}

安卓平台效果

using Android.Widget;
using Android.Graphics;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

using CustomStepper.Droid;

[assembly: ResolutionGroupName("CustomStepper")]
[assembly: ExportEffect(typeof(StepperColorEffect), nameof(StepperColorEffect))]
namespace CustomStepper.Droid
{
public class StepperColorEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Element is Stepper element && Control is LinearLayout control)
{
control.GetChildAt(0).Background.SetColorFilter(CustomStepper.StepperColorEffect.GetColor(element).ToAndroid(), PorterDuff.Mode.Multiply);
control.GetChildAt(1).Background.SetColorFilter(CustomStepper.StepperColorEffect.GetColor(element).ToAndroid(), PorterDuff.Mode.Multiply);
}
}

protected override void OnDetached()
{
if (Element is Stepper element && Control is LinearLayout control)
{
control.GetChildAt(0).Background.SetColorFilter(Xamarin.Forms.Color.Gray.ToAndroid(), PorterDuff.Mode.Multiply);
control.GetChildAt(1).Background.SetColorFilter(Xamarin.Forms.Color.Gray.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
}
}

截图

安卓

enter image description here

iOS

enter image description here

关于c# - 如何更改 iOS 和 Android 步进器的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48537576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com