问题
Hi I am using a Picker
to display some values to be select.
<Picker x:Name="selection" Title="Select Option">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Episode</x:String>
<x:String>All</x:String>
<x:String>Department</x:String>
<x:String>Section</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
This is not displaying when page load only Title
will display.
When I tap on title it will display the picker.
but I want to display picker when page load...
How can I do that?
回答1:
but I want to display picker when page load...
You could delete the Title to do the same thing.
Xaml:
<Picker x:Name="selection">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Episode</x:String>
<x:String>All</x:String>
<x:String>Department</x:String>
<x:String>Section</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
Result:
Updated:
If you want to make a embed picker, you could do this in custom renderer.
PickerViewRenderer.cs
public class PickerViewRenderer : ViewRenderer<PickerView, NumberPicker>
{
protected override void OnElementChanged(ElementChangedEventArgs<PickerView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(new NumberPicker(Context));
}
else
{
Control.ValueChanged -= Control_ValueChanged;
}
if (e.NewElement != null)
{
Control.ValueChanged += Control_ValueChanged;
UpdateItemsSource();
UpdateSelectedIndex();
UpdateFont();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == PickerView.ItemsSourceProperty.PropertyName)
{
UpdateItemsSource();
}
else if (e.PropertyName == PickerView.SelectedIndexProperty.PropertyName)
{
UpdateSelectedIndex();
}
else if (e.PropertyName == PickerView.FontFamilyProperty.PropertyName)
{
UpdateFont();
}
else if (e.PropertyName == PickerView.FontSizeProperty.PropertyName)
{
UpdateFont();
}
}
private void UpdateItemsSource()
{
var arr = new List<string>();
if (Element.ItemsSource != null)
{
foreach (var item in Element.ItemsSource)
{
arr.Add(item.ToString());
}
}
if (arr.Count > 0)
{
int newMax = arr.Count - 1;
if (newMax < Control.Value)
{
Element.SelectedIndex = newMax;
}
var extend = Control.MaxValue <= newMax;
if (extend)
{
Control.SetDisplayedValues(arr.ToArray());
}
Control.MaxValue = newMax;
Control.MinValue = 0;
if (!extend)
{
Control.SetDisplayedValues(arr.ToArray());
}
}
}
private void UpdateSelectedIndex()
{
if (Element.SelectedIndex < Control.MinValue || Element.SelectedIndex >= Control.MaxValue)
{
return;
}
Control.Value = Element.SelectedIndex;
}
void UpdateFont()
{
var font = string.IsNullOrEmpty(Element.FontFamily) ?
Font.SystemFontOfSize(Element.FontSize) :
Font.OfSize(Element.FontFamily, Element.FontSize);
SetTextSize(Control, font.ToTypeface(), (float)(Element.FontSize * Context.Resources.DisplayMetrics.Density));
}
void Control_ValueChanged(object sender, NumberPicker.ValueChangeEventArgs e)
{
Element.SelectedIndex = e.NewVal;
}
/// <summary>
/// NumberPicker
/// </summary>
/// <see cref="http://stackoverflow.com/questions/22962075/change-the-text-color-of-numberpicker"/>
/// <param name="numberPicker">Number picker.</param>
/// <param name="textSizeInSp">Text size in pixel.</param>
private static void SetTextSize(NumberPicker numberPicker, Typeface fontFamily, float textSizeInSp)
{
int count = numberPicker.ChildCount;
for (int i = 0; i < count; i++)
{
var child = numberPicker.GetChildAt(i);
var editText = child as EditText;
if (editText != null)
{
try
{
Field selectorWheelPaintField = numberPicker.Class
.GetDeclaredField("mSelectorWheelPaint");
selectorWheelPaintField.Accessible = true;
((Paint)selectorWheelPaintField.Get(numberPicker)).TextSize = textSizeInSp;
editText.Typeface = fontFamily;
editText.SetTextSize(ComplexUnitType.Px, textSizeInSp);
numberPicker.Invalidate();
}
catch (System.Exception e)
{
System.Diagnostics.Debug.WriteLine("SetNumberPickerTextColor failed.", e);
}
}
}
}
}
You could download the sample code from the link. https://github.com/amay077/Xamarin_Forms_PickerViewSample/tree/master/PickerViewSample
回答2:
As per my understanding you want to display picker values when page load.
protected override void OnAppearing()
{
base.OnAppearing();
if (selection.IsFocused)
selection.Unfocus();
else
selection.Focus();
}
回答3:
In my case I just put a timer running every second consulting the ItemSource in the code behind:
public Validate ()
{
InitializeComponent ();
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
if (picker.ItemsSource != null)
{
picker.Focus();
return false;
}
else
{
return true;
}
});
}
This timer going to run while the ItemsSource == null.
来源:https://stackoverflow.com/questions/59297317/how-to-display-picker-when-page-load-without-tapping-on-title-xamarin-forms