如何在页面加载时显示选择器而不点击标题 Xamarin.Forms [英] How to Display Picker when page load without tapping on title Xamarin.Forms

查看:24
本文介绍了如何在页面加载时显示选择器而不点击标题 Xamarin.Forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Picker 来显示一些要选择的值.

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>

页面加载时不显示,仅显示Title.

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...

我该怎么做?

推荐答案

但我想在页面加载时显示选择器...

but I want to display picker when page load...

你可以删除标题来做同样的事情.

You could delete the Title to do the same thing.

XML:

<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>

结果:

更新:

如果你想制作一个嵌入选择器,你可以在自定义渲染器中做到这一点.

If you want to make a embed picker, you could do this in custom renderer.

PickerViewRenderer.cs

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);
                }
            }
        }
    }
}

您可以从链接下载示例代码.https://github.com/amay077/Xamarin_Forms_PickerViewSample/tree/master/PickerViewSample

You could download the sample code from the link. https://github.com/amay077/Xamarin_Forms_PickerViewSample/tree/master/PickerViewSample

这篇关于如何在页面加载时显示选择器而不点击标题 Xamarin.Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆