Xamarin.Forms 选择器默认值

Xamarin.Forms Picker Default Value

我有日期选择器,我正在通过网络服务动态填充它。

<Picker x:Name="picker" Title="Select Something" ItemsSource="{Binding}" ItemDisplayBinding="{Binding Name}"></Picker>

我如上所述设置 Title 属性 但对于 UWP,它显示为标签。我如何将其设置为 UWP 的默认 value/prompt?

如有任何帮助,我们将不胜感激。

正如 Digitalsa1nt 所说,Picker 没有 PlaceholderText 属性。但是,您可以通过 custom renderer 添加此 属性。

CustomPicker.cs

public class CustomPicker : Picker
{
    public static readonly BindableProperty PlaceHolderProperty = BindableProperty.Create(
propertyName: "PlaceHolder",
eturnType: typeof(string),
declaringType: typeof(CustomPicker),
defaultValue: default(string));

    public string PlaceHolder
    {
        get { return (string)GetValue(PlaceHolderProperty); }
        set { SetValue(PlaceHolderProperty, value); }
    }

}

在您的 uwp 客户端项目中实施 CustomPickerRenderer

CustomPickerRenderer.cs

[assembly: ExportRenderer(typeof(CustomPicker), typeof(CustomPickerRenderer))]

namespace XamarinPickerDefaultValueTest.UWP
{
    public class CustomPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            var element = Element as CustomPicker;

            if (Control != null)
            {
                Control.PlaceholderText = element.PlaceHolder;
            }
        }

    }
}

用法

<local:CustomPicker PlaceHolder="Picker something" x:Name="picker"/>