【问题标题】:Xamarin forms Picker on android change Cancel textXamarin 在 android 上形成 Picker 更改取消文本
【发布时间】:2018-02-23 21:44:11
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

  • 提交拉取请求
  • 然后替换代码?并将其包含在我的项目中而不是 xamarin nuget 包中?
  • 你能用你自己的渲染器吗?几乎只是从 Xamarin 复制粘贴实现并对其进行扩展以更改文本。
  • 仅仅为了改变一个字符串值并不是改造整辆自行车的好主意
  • 嗯,很明显你有两个选择,要么做一个 PR 并等待它被官方存储库接受,要么你自己实现并使用它?

标签: c# android xamarin.forms xamarin.android picker


【解决方案1】:

Android 上的 Xamarin 表单选取器更改取消文本

作为替代选择,您可以在 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.

【讨论】:

  • 在上面的解决方案中添加了以下 sn-p 以禁用在选择器对话框外单击。 _dialog.SetCancelable(false); _dialog.SetCanceledOnTouchOutside(false);
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多