如何在 Xamarin Mac 平台上更改滑块的颜色?

How to change Color from Slider on Xamarin Mac Platform?

Xamarin Mac 项目上 Slider 的默认颜色是蓝色,我想将其更改为绿色,所以我做了一个自定义渲染器,但不幸的是,我不知道在里面做什么自定义渲染器,如何更改颜色?

using CustomSliderColor.MacOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;

[assembly: ExportRenderer(typeof(CustomSlider), typeof(CustomSliderRenderer))]
namespace CustomSliderColor.MacOS
{
    public class CustomSliderRenderer : SliderRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                //What do I put here?????
            }
        }
    }
}

我找到了我自己问题的答案。

先分class "NSSliderCell" class,这里举个例子:

using System;
using AppKit;
using CoreGraphics;
using Foundation;

namespace myproject.MacOs
{
    public class CustomSliderCell : NSSliderCell 
    {
        public override void DrawBar(CGRect aRect, Boolean flipped)
        {
            CGRect rect = aRect;
            rect.Size = new CGSize(rect.Size.Width, 5);
            float barRadius = 2.5f;
            double value = (double)((this.DoubleValue - this.MinValue) / (this.MaxValue - this.MinValue));
            float finalWidth = (float)(value * (this.ControlView.Frame.Size.Width - 8));
            CGRect leftRect = rect;
            leftRect.Size = new CGSize(finalWidth, leftRect.Size.Height);
            NSBezierPath bg = new NSBezierPath();
            bg.AppendPathWithRoundedRect(rect, barRadius, barRadius);
            NSColor.FromRgb(0, 160, 0).SetFill();
            bg.Fill();
            NSBezierPath active = new NSBezierPath();
            active.AppendPathWithRoundedRect(leftRect, barRadius, barRadius);
            NSColor.FromRgb(0,128,0).SetFill();
            active.Fill();
        }

    }
}

然后在 Mac

上的 customSlider 渲染器中
using AppKit;
using CustomSliderColor.MacOS;
using Xamarin.Forms;
using myproject;
using myproject.MacOs;
using Xamarin.Forms.Platform.MacOS;

[assembly: ExportRenderer(typeof(CustomSlider), typeof(CustomSliderRenderer))]
namespace CustomSliderColor.MacOS
{
    public class CustomSliderRenderer : SliderRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
        {
            if (Control == null)
            {
                NSSlider customNSSlider = new NSSlider();
                CustomSliderCell custom = new CustomSliderCell();
                customNSSlider.Cell = custom;
                SetNativeControl(customNSSlider);
            }
            base.OnElementChanged(e);
        }
    }
}

然后你就拥有了!!!