gpt4 book ai didi

c# - Xamarin forms Picker on android更改取消文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:10 26 4
gpt4 key购买 nike

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

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

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

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

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

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

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

最佳答案

Xamarin forms Picker on android change Cancel text

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

关于c# - Xamarin forms Picker on android更改取消文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46227028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com