C# 如何更改更改取消和确定按钮';选择器中的颜色?

C# 如何更改更改取消和确定按钮';选择器中的颜色?,c#,xamarin.forms,colors,picker,C#,Xamarin.forms,Colors,Picker,我正在应用程序中使用custompicker,我需要更改“取消”和“确定”按钮的颜色。我最近更改了android的timepicker和datepicker的整个主题,将以下代码添加到样式文件中: <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item> <item name="android:timePickerDialogTheme">@style/

我正在应用程序中使用custompicker,我需要更改“取消”和“确定”按钮的颜色。我最近更改了android的timepicker和datepicker的整个主题,将以下代码添加到样式文件中:

 <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
    <item name="android:timePickerDialogTheme">@style/TimePickerTheme</item>

item name="colorAccent">#039BE5</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
  </style>
  <style name="TimePickerTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#039BE5</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
  </style>
@style/AppCompatDialogStyle
@样式/时间选择器主题
item name=“colorAccent”>#039BE5
包装内容
包装内容
#039BE5
包装内容
包装内容

这是我可以为选择器添加的类似代码吗?

如果更改主题
\039BE5
可能会影响其他控件的主题,则只能在CustomRenderer中更改颜色,如下所示:

public class CustomPickerRenderer : PickerRenderer
{
    private Context context;
    private IElementController ElementController => Element as IElementController;
    private AlertDialog _dialog;

    public CustomPickerRenderer(Context context) : base(context)
    {
        this.context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        if (Control == null || e.NewElement == null) return;
        Control.Click += Control_Click1;
    }
    protected override void Dispose(bool disposing)
    {
        Control.Click -= Control_Click1;
        base.Dispose(disposing);
    }

    private void Control_Click1(object sender, EventArgs e)
    {
        Picker model = Element;

        var picker = new NumberPicker(Context);
        if (model.Items != null && model.Items.Any())
        {
            picker.MaxValue = model.Items.Count - 1;
            picker.MinValue = 0;
            picker.SetDisplayedValues(model.Items.ToArray());
            picker.WrapSelectorWheel = false;
            picker.DescendantFocusability = DescendantFocusability.BlockDescendants;
            picker.Value = model.SelectedIndex;
        }

        var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
        layout.AddView(picker);

        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);

        var builder = new AlertDialog.Builder(Context);
        builder.SetView(layout);
        builder.SetTitle(model.Title ?? "");

        //change the text or color here
        builder.SetNegativeButton(Html.FromHtml("<font color='#039BE5'>Cancel</font>"), (s, a) =>
        {
            ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
            // It is possible for the Content of the Page to be changed when Focus is changed.
            // In this case, we'll lose our Control.
            Control?.ClearFocus();
            _dialog = null;
        });

         //change the text or color here
        builder.SetPositiveButton(Html.FromHtml("<font color='#039BE5'>OK</font>"), (s, a) =>
        {
            ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
            // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
            // In this case, the Element & Control will no longer exist.
            if (Element != null)
            {
                if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                    Control.Text = model.Items[Element.SelectedIndex];
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                // It is also possible for the Content of the Page to be changed when Focus is changed.
                // In this case, we'll lose our Control.
                Control?.ClearFocus();
            }
            _dialog = null;
        });

        _dialog = builder.Create();
        _dialog.DismissEvent += (ssender, args) =>
        {
            ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
        };
        _dialog.Show();
    }
}
公共类CustomPickerRenderer:PickerRenderer
{
私人语境;
私有IElementController元素Controller=>元素作为IElementController;
私有AlertDialog\u对话框;
公共CustomPickerRenderer(上下文):基础(上下文)
{
this.context=上下文;
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(Control==null | | e.NewElement==null)返回;
控件。单击+=控件\u单击1;
}
受保护的覆盖无效处置(布尔处置)
{
控件。单击-=控件\u单击1;
基地。处置(处置);
}
私有无效控件\u Click1(对象发送方,事件参数e)
{
选择器模型=元件;
var picker=newnumberpicker(上下文);
if(model.Items!=null&&model.Items.Any())
{
picker.MaxValue=model.Items.Count-1;
picker.MinValue=0;
picker.SetDisplayedValues(model.Items.ToArray());
picker.WrapSelectorWheel=假;
picker.genderantfocusability=genderantfocusability.blocksubstands;
picker.Value=model.SelectedIndex;
}
变量布局=新线性布局(上下文){Orientation=Orientation.Vertical};
布局。添加视图(选择器);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,true);
var builder=新建AlertDialog.builder(上下文);
builder.SetView(布局);
builder.SetTitle(model.Title??);
//在此处更改文本或颜色
builder.SetNegativeButton(Html.FromHtml(“取消”),(s,a)=>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
//当焦点改变时,页面内容可能会改变。
//在这种情况下,我们将失去控制。
控件?.ClearFocus();
_dialog=null;
});
//在此处更改文本或颜色
builder.SetPositiveButton(Html.FromHtml(“OK”),(s,a)=>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty,Picker.Value);
//可以在SelectedIndexChanged上更改页面内容。
//在这种情况下,元素和控件将不再存在。
if(元素!=null)
{
如果(model.Items.Count>0&&Element.SelectedIndex>=0)
Control.Text=model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
//当焦点改变时,页面内容也可能改变。
//在这种情况下,我们将失去控制。
控件?.ClearFocus();
}
_dialog=null;
});
_dialog=builder.Create();
_dialog.DismissEvent+=(ssender,args)=>
{
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
};
_dialog.Show();
}
}
更新 我在平板电脑模拟器上进行了测试,效果很好


这种方式在android v9.0上运行良好,但我在我的两台平板电脑上尝试过,但没有改变:(稍后我将测试它并告诉您you@mohammadanouti我测试了它,它工作了,你可以看到上面的gif。你的平板电脑是什么?当在新项目上添加代码时,它给了我这个错误:成员'DegenantFocusability.BlockDegenders'不能用实例引用访问;改为用类型名限定它。在这行:picker.genderantfocusability=genderantfocusability.blocksubstands;我在华为PLE-701L和三星SM-T365上试用过,它们都是“安卓5.1”,没有任何变化澄清:这个问题和公认的答案都是针对(Xamarin)的Android;它不是设置选择器按钮颜色的跨平台解决方案。