Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何独立于操作系统设置更改选择器负按钮文本_C#_Xamarin.forms_Localization_Xamarin.android - Fatal编程技术网

C# 如何独立于操作系统设置更改选择器负按钮文本

C# 如何独立于操作系统设置更改选择器负按钮文本,c#,xamarin.forms,localization,xamarin.android,C#,Xamarin.forms,Localization,Xamarin.android,我有一个只使用英语的应用程序,但我手机的操作系统是西班牙语的。我已经在我的页面中添加了一个选择器,如果我点击它来更改它的值,我会看到取消按钮文本是西班牙语的。我想用英语显示这个按钮。我之所以使用Xamarin.Forms,是因为我更容易使用它进行开发,但我只需要知道如何才能在Android中进行此更改。我尝试在MainActivity中的OnCreate()方法中更改区域性,但它不起作用(在调试和发布时运行) 然后,我尝试了Android特定的代码(同样在main活动中的OnCreate()方法

我有一个只使用英语的应用程序,但我手机的操作系统是西班牙语的。我已经在我的页面中添加了一个
选择器
,如果我点击它来更改它的值,我会看到取消按钮文本是西班牙语的。我想用英语显示这个按钮。我之所以使用Xamarin.Forms,是因为我更容易使用它进行开发,但我只需要知道如何才能在Android中进行此更改。我尝试在
MainActivity
中的
OnCreate()
方法中更改区域性,但它不起作用(在调试和发布时运行)

然后,我尝试了Android特定的代码(同样在
main活动中的
OnCreate()
方法中):

但它不起作用。按钮的文本仍然是西班牙语。在这种情况下,我想更改特定的文本,但没有公共属性允许我更改它(即picker.CancelButtonText)。我想显示“取消”而不是“取消”。你知道我怎样才能改变这段文字吗

我检查了文件,发现了以下代码:

var builder = new AlertDialog.Builder(Context);
builder.SetView(layout);
builder.SetTitle(model.Title ?? "");
builder.SetNegativeButton(global::Android.Resource.String.Cancel, (s, a) =>
{
    ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, 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;
});
global::Android.Resource.String.Cancel
是一个
int
常量,所以我无法更改它。你知道你是否可以访问负片按钮文本吗

谢谢,, 乔恩

global::Android.Resource.String.Cancel是一个int常量,因此我无法更改它。你知道你是否可以访问负片按钮文本吗

您的方向是正确的,但不幸的是,目前用户无法访问
选择器的
警报对话框
,因此无法自定义或自定义本地化
取消
按钮文本


我现在能看到的唯一选项是使用自定义渲染器创建您自己的
选取器
控件,并自己构建
警报对话框
,然后您可以通过
警报对话框获取按钮。GetButton

我已成功设置了负按钮上的文本。我创建了
CustomPicker
CustomPickerRenderer

CustomPicker.cs(无特殊要求):

CustomPickerRenderer.cs(继承并复制了其中的一些方法,只做了一些更改):

使用Android.App;
使用Android.Content;
使用Android.Content.Res;
使用Android.Text;
使用Android.Widget;
使用renderTest.Droid.Renderers;
使用制度;
使用System.Collections.Specialized;
使用System.Linq;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
[程序集:ExportRenderer(typeof(renderTest.Controls.CustomPicker),typeof(CustomPickerRenderer))]
命名空间renderTest.Droid.Renderers
{
公共类CustomPickerRenderer:Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
{
AlertDialog\u对话框;
TextColorSwitcher\u TextColorSwitcher;
公共CustomPickerRenderer(上下文):基(上下文){}
//此方法是设置OnClickListener、复制和删除base.OnElementChanged(e);行所必需的
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
if(e.OldElement!=null)
((INotifyCollectionChanged)e.OldElement.Items)。CollectionChanged-=RowsCollectionChanged;
if(例如NewElement!=null)
{
((INotifyCollectionChanged)e.NewElement.Items).CollectionChanged+=RowsCollectionChanged;
if(Control==null)
{
EditText文本字段=CreateNativeControl();
textField.Focusable=false;
textField.Clickable=true;
textField.Tag=this;
textField.InputType=InputTypes.Null;
SetOnClickListener(PickerListener.Instance);
_textColorSwitcher=新的textColorSwitcher(textField.TextColors);
SetNativeControl(文本字段);
}
更新Epicker();
UpdateTextColor();
}
}
//此方法是更改负片按钮文本所必需的。
void OnClick()
{
选择器模型=元件;
如果(_dialog==null)
{
使用(var builder=new AlertDialog.builder(上下文))
{
builder.SetTitle(model.Title??);
string[]items=model.items.ToArray();
SetItems(items,(s,e)=>((IElementController)模型).SetValueFromRenderer(Picker.SelectedIndexProperty,e.Which));
SetNegativeButton(“取消”,(o,args)=>{};//更改负按钮文本
((IElementController)元素).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey,true);
_dialog=builder.Create();
}
_对话框。SetCanceledOnTouchOutside(true);
_dialog.DismissEvent+=(发送方,参数)=>
{
(元素作为IElementController)?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey,false);
_dialog.Dispose();
_dialog=null;
};
_dialog.Show();
}
}
void RowsCollectionChanged(对象发送方,事件参数e)
{
更新Epicker();
}
void UpdatePicker()
{
Control.Hint=Element.Title;
if(Element.SelectedIndex=-1 | | Element.Items==null)
Control.Text=null;
其他的
Control.Text=Element.Items[Element.SelectedIndex];
}
void UpdateTextColor()
{
_textColorSwitcher?.UpdateTextColor(控件,元素.TextColor);
}
//侦听器已更改为使用CustomPickerRenderer
类PickerListener:Java.Lang.Object,IOnClickListener
{
#区域静校正
Java.Util.Locale.Default = new Java.Util.Locale("en");
var builder = new AlertDialog.Builder(Context);
builder.SetView(layout);
builder.SetTitle(model.Title ?? "");
builder.SetNegativeButton(global::Android.Resource.String.Cancel, (s, a) =>
{
    ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, 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;
});
public class CustomPicker : Picker {}
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Text;
using Android.Widget;
using RendererTest.Droid.Renderers;
using System;
using System.Collections.Specialized;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(RendererTest.Controls.CustomPicker), typeof(CustomPickerRenderer))]
namespace RendererTest.Droid.Renderers
{
    public class CustomPickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
    {
        AlertDialog _dialog;
        TextColorSwitcher _textColorSwitcher;

        public CustomPickerRenderer(Context context) : base(context) { }

        // This method is necessary to set the OnClickListener, copied removing base.OnElementChanged(e); line
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            if (e.OldElement != null)
                ((INotifyCollectionChanged)e.OldElement.Items).CollectionChanged -= RowsCollectionChanged;

            if (e.NewElement != null)
            {
                ((INotifyCollectionChanged)e.NewElement.Items).CollectionChanged += RowsCollectionChanged;
                if (Control == null)
                {
                    EditText textField = CreateNativeControl();
                    textField.Focusable = false;
                    textField.Clickable = true;
                    textField.Tag = this;
                    textField.InputType = InputTypes.Null;
                    textField.SetOnClickListener(PickerListener.Instance);
                    _textColorSwitcher = new TextColorSwitcher(textField.TextColors);
                    SetNativeControl(textField);
                }
                UpdatePicker();
                UpdateTextColor();
            }
        }

        // This method is necessary to change negative button text.
        void OnClick()
        {
            Picker model = Element;
            if (_dialog == null)
            {
                using (var builder = new AlertDialog.Builder(Context))
                {
                    builder.SetTitle(model.Title ?? "");
                    string[] items = model.Items.ToArray();
                    builder.SetItems(items, (s, e) => ((IElementController)model).SetValueFromRenderer(Picker.SelectedIndexProperty, e.Which));

                    builder.SetNegativeButton("Cancel", (o, args) => { }); // Changing negative button text

                    ((IElementController)Element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);

                    _dialog = builder.Create();
                }
                _dialog.SetCanceledOnTouchOutside(true);
                _dialog.DismissEvent += (sender, args) =>
                {
                    (Element as IElementController)?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
                    _dialog.Dispose();
                    _dialog = null;
                };

                _dialog.Show();
            }
        }

        void RowsCollectionChanged(object sender, EventArgs e)
        {
            UpdatePicker();
        }

        void UpdatePicker()
        {
            Control.Hint = Element.Title;

            if (Element.SelectedIndex == -1 || Element.Items == null)
                Control.Text = null;
            else
                Control.Text = Element.Items[Element.SelectedIndex];
        }

        void UpdateTextColor()
        {
            _textColorSwitcher?.UpdateTextColor(Control, Element.TextColor);
        }

        // The listener is changed to work with CustomPickerRenderer
        class PickerListener : Java.Lang.Object, IOnClickListener
        {
            #region Statics

            public static readonly PickerListener Instance = new PickerListener();

            #endregion

            public void OnClick(global::Android.Views.View v)
            {
                var renderer = v.Tag as CustomPickerRenderer; // Work with my renderer
                renderer?.OnClick();
            }
        }
    }
}