Forms 如何更改选择器的颜色';这是下划线吗?
我用Xamarin表格写申请表。我使用自定义渲染器将“entry”下划线颜色设置为透明。我还需要以同样的方式将选择器底线的颜色设置为透明,但它不起作用。我在这里做了一个例子:将Control.BackgroundColor设置为transparent,但没有效果。在Xamarin.Android styles.xml中,有重音颜色需要更改,但当点击它时,它只会更改底线的颜色,并且对所有应用程序都有影响(这很糟糕)。下划线保留常规的黑色。是否有其他解决方案,或者最好放弃Xamarin Forms的选取器,创建自己的自定义弹出窗口?您可以使用CustomRenderer 在iOS中Forms 如何更改选择器的颜色';这是下划线吗?,forms,xamarin,picker,Forms,Xamarin,Picker,我用Xamarin表格写申请表。我使用自定义渲染器将“entry”下划线颜色设置为透明。我还需要以同样的方式将选择器底线的颜色设置为透明,但它不起作用。我在这里做了一个例子:将Control.BackgroundColor设置为transparent,但没有效果。在Xamarin.Android styles.xml中,有重音颜色需要更改,但当点击它时,它只会更改底线的颜色,并且对所有应用程序都有影响(这很糟糕)。下划线保留常规的黑色。是否有其他解决方案,或者最好放弃Xamarin Forms的
使用系统;
使用App1.iOS;
使用基础;
使用UIKit;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
[assembly:ExportRenderer(typeof(Picker),typeof(MyiOSPicker))]
名称空间App1.iOS
{
公共类MyiOSPicker:PickerRenderer、IUIPickerViewDelegate、IUIPickerViewDataSource
{
字符串SelectedValue;
公共MyiOSPicker()
{
}
公共nint GetComponentCount(UIPickerView pickerView)
{
返回1;
}
公共nint GetRowsInComponent(UIPickerView pickerView,nint组件)
{
返回元素.Items.Count;
}
[导出(“pickerView:viewForRow:forComponent:reusingView:”)]
公共UIView GetView(UIPickerView-pickerView、nint行、nint组件、UIView视图)
{
foreach(pickerView.subview中的UIView行)
{
如果(行、帧、高度)
{
Control.Text=SelectedValue;
toolbar.RemoveFromSuperview();
pickerView.RemoveFromSuperview();
Control.ResignFirstResponder();
});
toolbar.Items=newuibarbuttonItem[]{done};
}
}
}
}
在Android中
使用系统;
使用System.Linq;
使用Java.Lang.Reflect;
使用Android.App;
使用Android.Content;
使用Android.Views;
使用Android.Widget;
使用App1.Droid;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
使用Android.Graphics.Drawables;
[assembly:ExportRenderer(typeof(Picker)、typeof(MyAndroidPicker))]
名称空间App1.Droid
{
公共级MyAndroidPicker:PickerRenderer
{
IElementController元素控制器=>元素作为IElementController;
私有AlertDialog\u对话框;
公共MyAndroidPicker(上下文):基础(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.NewElement==null | | e.OldElement!=null)
返回;
Control.Click+=Control\u Click;
}
受保护的覆盖无效处置(布尔处置)
{
Control.Click-=Control\u Click;
基地。处置(处置);
}
私有void SetPickerDividerColor(TextColorNumberPicker选择器)
{
Field[]fields=picker.Class.GetDeclaredFields();
foreach(字段中的字段pf)
{
if(pf.Name.Equals(“mSelectionDivider”))
{
pf.Accessible=true;
//在这里设置线条颜色!!!
pf.Set(选择器,新的可着色(Android.Graphics.Color.Yellow));
}
}
}
私有无效控件\u单击(对象发送方,事件参数e)
{
选择器模型=元件;
var picker=newtextColorNumberPicker(上下文);
if(model.Items!=null&&model.Items.Any())
{
picker.MaxValue=model.Items.Count-1;
picker.MinValue=0;
//SetBackgroundColor(Android.Graphics.Color.Yellow);
picker.SetDisplayedValues(model.Items.ToArray());
//设置DisplayedValues后调用该方法
picker.WrapSelectorWheel=假;
picker.Value=model.SelectedIndex;
SetPickerDividerColor(选择器);
}
变量布局=新线性布局(上下文){Orientation=Orientation.Vertical};
布局。添加视图(选择器);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,true);
var builder=新建AlertDialog.builder(上下文);
builder.SetView(布局);
builder.SetTitle(model.Title??);
builder.SetNegativeButton(“取消”,(s,a)=>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
//当焦点改变时,页面内容可能会改变。
//在这种情况下,我们将失去控制。
控件?.ClearFocus();
_dialog=null;
});
builder.SetPositiveButton(“确定”,(s,a)=>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty,Picker.Value);
//可以在SelectedIndexChanged上更改页面内容。
//在这种情况下,元素和控件将不再存在。
if(元素!=null)
{
如果(model.Items.Count>0&&Element.SelectedIndex>=0)
Control.Text=model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
//当焦点改变时,页面内容也可能改变。
//在这种情况下,我们将失去控制。
控件?.ClearFocus();
}
_dialog=nu
using System;
using App1.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Picker), typeof(MyiOSPicker))]
namespace App1.iOS
{
public class MyiOSPicker : PickerRenderer, IUIPickerViewDelegate, IUIPickerViewDataSource
{
string SelectedValue;
public MyiOSPicker()
{
}
public nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
public nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
return Element.Items.Count;
}
[Export("pickerView:viewForRow:forComponent:reusingView:")]
public UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
{
foreach(UIView line in pickerView.Subviews)
{
if(line.Frame.Height<1)
{
line.BackgroundColor = UIColor.Red; // set line coloer here!!!!
}
}
UILabel label = new UILabel
{
//here you can set the style of item!!!
TextColor = UIColor.Blue,
Text = Element.Items[(int)row].ToString(),
TextAlignment = UITextAlignment.Center,
};
return label;
}
[Export("pickerView:didSelectRow:inComponent:")]
public void Selected(UIPickerView pickerView, nint row, nint component)
{
Control.Text = Element.Items[(int)row];
SelectedValue = Element.Items[(int)row];
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
SelectedValue = Element.Items[0];
UIPickerView pickerView = (UIPickerView)Control.InputView;
pickerView.WeakDelegate = this;
pickerView.DataSource = this;
UIToolbar toolbar = (UIToolbar)Control.InputAccessoryView;
UIBarButtonItem done = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>
{
Control.Text = SelectedValue;
toolbar.RemoveFromSuperview();
pickerView.RemoveFromSuperview();
Control.ResignFirstResponder();
});
toolbar.Items = new UIBarButtonItem[] { done };
}
}
}
}
using System;
using System.Linq;
using Java.Lang.Reflect;
using Android.App;
using Android.Content;
using Android.Views;
using Android.Widget;
using App1.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Graphics.Drawables;
[assembly: ExportRenderer(typeof(Picker), typeof(MyAndroidPicker))]
namespace App1.Droid
{
public class MyAndroidPicker : PickerRenderer
{
IElementController ElementController => Element as IElementController;
private AlertDialog _dialog;
public MyAndroidPicker(Context context) : base(context)
{
}
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 SetPickerDividerColor(TextColorNumberPicker picker)
{
Field[] fields = picker.Class.GetDeclaredFields();
foreach (Field pf in fields)
{
if (pf.Name.Equals("mSelectionDivider"))
{
pf.Accessible = true;
// set the line color here!!!
pf.Set(picker, new ColorDrawable(Android.Graphics.Color.Yellow));
}
}
}
private void Control_Click(object sender, EventArgs e)
{
Picker model = Element;
var picker = new TextColorNumberPicker(Context);
if (model.Items != null && model.Items.Any())
{
picker.MaxValue = model.Items.Count - 1;
picker.MinValue = 0;
// picker.SetBackgroundColor(Android.Graphics.Color.Yellow);
picker.SetDisplayedValues(model.Items.ToArray());
//call the method after you setting DisplayedValues
picker.WrapSelectorWheel = false;
picker.Value = model.SelectedIndex;
SetPickerDividerColor(picker);
}
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 ", (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();
}
}
public class TextColorNumberPicker: NumberPicker
{
public TextColorNumberPicker(Context context) : base(context)
{
}
public override void AddView(Android.Views.View child, int index, ViewGroup.LayoutParams @params)
{
base.AddView(child, index, @params);
UpdateView(child);
}
public void UpdateView(Android.Views.View view)
{
if ( view is EditText ) {
//set the font of text
((EditText)view).TextSize = 12;
}
}
}
}