Xamarin在Android上的Picker更改取消文本 [英] Xamarin forms Picker on android change Cancel text

查看:328
本文介绍了Xamarin在Android上的Picker更改取消文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xamarin.Forms在Android上.单击选择器将打开对话框,并且否定按钮的默认文本为取消".我该如何更改?

Xamarin.Forms on Android. Clicking picker opens dialog and the negative button has default text of "Cancel". How can I change it?

我查看了Xamarin的开源项目,他们设置了像这样的肯定按钮文本

I looked in open source project of Xamarin and they set the positive button text like this

builder.SetNegativeButton(global::Android.Resource.String.Cancel, (s, a) => ...

此方法是私有的,因此我无法覆盖类方法.

This method is private so I cant override the class method.

我也不能复制此类的粘贴实现,因为它的成员是Xamarn dll-s专有的...

Neither I can copy paste implementation of this class because its members are private to Xamarn dll-s...

链接到Xamarin.Forms andoid上的该选择器实现:

Link to that picker implementation on Xamarin.Forms andoid:

https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Renderers/PickerRenderer.cs

推荐答案

Xamarin在Android上更改Picker取消文本

Xamarin forms Picker on android change Cancel text

作为替代选择,您可以在PickerRenderer中重写整个对话框:

As an alternative choice, you could rewrite the whole dialog in your PickerRenderer :

public class MyPickerRenderer : Xamarin.Forms.Platform.Android.PickerRenderer
{
    private IElementController ElementController => Element as IElementController;
    private AlertDialog _dialog;

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

        if (e.NewElement == null || e.OldElement != null)
            return;

        Control.Click += Control_Click;
    }

    protected override void Dispose(bool disposing)
    {
        Control.Click -= Control_Click;
        base.Dispose(disposing);
    }

    private void Control_Click(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 ?? "");
        builder.SetNegativeButton("Cancel =-= ", (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;
        });
        builder.SetPositiveButton("Ok 0.0", (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();
    }
}

效果.

这篇关于Xamarin在Android上的Picker更改取消文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆