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

时间:2018-01-31 08:44:51

标签: xamarin xamarin.forms

我使用的代码创建了一个如下所示的选择器:

<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>

1 个答案:

答案 0 :(得分:2)

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

以下是您需要做的一些更改:

BindableProperty

上创建一个SegmentedControl
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 ... */
}

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

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;
}