Xamarin在android更改取消文本上形成Picker

问题描述 投票:9回答:2

Android上的Xamarin.Forms。单击选择器打开对话框,否定按钮的默认文本为“取消”。我该怎么改变它?

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

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

这个方法是私有的,所以我不能覆盖类方法。

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

链接到Xamarin.Forms android中的选择器实现:

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

c# android xamarin.forms xamarin.android picker
2个回答
4
投票

Xamarin在android更改取消文本上形成Picker

作为替代选择,您可以在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();
    }
}

Effect


0
投票

由于对话框本身不是直接在XAML中使用的图形元素,因此没有任何东西阻止您直接在项目中使用Android代码。如果使用共享项目,就像使用条件编译一样简单,如果使用标准库,则需要使用依赖项服务。

在XAML方面,您可以使用一种自定义的EntryRenderer在屏幕上显示所选项目,这就是Xamarin无论如何都要做的。

© www.soinside.com 2019 - 2024. All rights reserved.