gpt4 book ai didi

xamarin - 如何将 iOS 中 Selector Rendere 的颜色更改为新颜色?

转载 作者:行者123 更新时间:2023-12-04 12:45:25 26 4
gpt4 key购买 nike

我正在使用创建一个看起来像这样的选择器的代码:

<local:CustomSwitch

我想要做的是更改代码,以便将蓝色全部更改为 XAML 中指定的颜色。有没有人对如何做到这一点有任何想法?

我使用的 XAML 如下所示:

<local:SegmentedControl ValueChanged="OnModeChanged" x:Name="segControlMode" HorizontalOptions="End">
<local:SegmentedControl.Children>
<local:SegmentedControlOption Text="Learn" />
<local:SegmentedControlOption Text="Quiz" />
</local:SegmentedControl.Children>
</local:SegmentedControl>

iOS渲染器:

using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using System.Diagnostics;
using System;

[assembly: ExportRenderer(typeof(Japanese.SegmentedControl), typeof(Japanese.iOS.SegmentedControlRenderer))]
namespace Japanese.iOS
{
public class SegmentedControlRenderer : ViewRenderer<SegmentedControl, UISegmentedControl>
{
protected override void OnElementChanged(ElementChangedEventArgs<SegmentedControl> e)
{
base.OnElementChanged(e);

UISegmentedControl segmentedControl = null;
if (Control == null)
{
segmentedControl = new UISegmentedControl();

for (var i = 0; i < e.NewElement.Children.Count; i++)
{
segmentedControl.InsertSegment(Element.Children[i].Text, i, false);
}

SetNativeControl(segmentedControl);
SetSelectedSegment();
}

if (e.OldElement != null)
{
// Unsubscribe from event handlers and cleanup any resources
if (segmentedControl != null)
segmentedControl.ValueChanged -= NativeValueChanged;
}

if (e.NewElement != null)
{
// Configure the control and subscribe to event handlers
segmentedControl.ValueChanged += NativeValueChanged;
}
}

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (e.PropertyName == nameof(SegmentedControl.SelectedSegment))
SetSelectedSegment();
}

void NativeValueChanged(object sender, EventArgs e)
{
if (Element is SegmentedControl formsElement)
{
formsElement.SelectedSegment = (int)Control.SelectedSegment;
};
}

void SetSelectedSegment()
{
if (Element is SegmentedControl formsElement)
{
if (formsElement.SelectedSegment >= 0 && formsElement.SelectedSegment < Control.NumberOfSegments)
Control.SelectedSegment = formsElement.SelectedSegment;
}
}
}
}

我想做的是在 XAML 中更改类似这样的颜色,例如:

<local:SegmentedControl ValueChanged="OnModeChanged" x:Name="segControlMode" HorizontalOptions="End" Color="Red" >
<local:SegmentedControl.Children>
<local:SegmentedControlOption Text="Learn" />
<local:SegmentedControlOption Text="Quiz" />
</local:SegmentedControl.Children>
</local:SegmentedControl>

最佳答案

您可以创建一个 BindableProperty在共享项目类上并在渲染器上处理其更改。

以下是您必须做的一些更改:

创建一个 BindablePropertySegmentedControl类(class)

public class SegmentedControl : Xamarin.Forms.View /* Replace this with your real inheritance */
{
/* ... The rest of your class ... */

public static readonly BindableProperty TintColorProperty = BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(SegmentedControl), Color.Blue, BindingMode.OneWay);
public Color TintColor
{
get { return (Color)GetValue(TintColorProperty); }
set { SetValue(TintColorProperty, value); }
}

/* ... The rest of your class ... */
}

然后将所选颜色传输到渲染器方法上的 native 控件等效属性 :

protected override void OnElementChanged(ElementChangedEventArgs<SegmentedControl> e)
{
base.OnElementChanged(e);

/* ... Your previous code as it is now ...*/

segmentedControl.TintColor = e.NewElement?.TintColor.ToUIColor();

SetNativeControl(segmentedControl);
SetSelectedSegment();

/* ... Your further code as it is now ...*/
}

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (e.PropertyName == nameof(SegmentedControl.SelectedSegment))
SetSelectedSegment();

/* Keep one eye on changes after rendered */
if(e.PropertyName == SegmentedControl.TintColorProperty.PropertyName)
SetSegmentTintColor();
}

void SetSegmentTintColor()
{
if (Element is SegmentedControl formsElement)
Control.TintColor = formsElement.TintColor;
}

关于xamarin - 如何将 iOS 中 Selector Rendere 的颜色更改为新颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48537908/

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