Bindable Picker的标题不起作用

时间:2016-05-10 17:33:57

标签: c# xaml xamarin.forms

我正在开发一个Xamarin项目,我需要使用一个选择器,因此Xamarin的原生选择器没有ItemsSource。我发现一个实现工作得很好,这里是:

using System;
using System.Collections;
using System.Linq;
using Xamarin.Forms;

namespace AnyNameSpace.Mobile.CustomControls
{
   public class BindablePicker : Picker
   {
       public static readonly BindableProperty ItemsSourceProperty =
             BindableProperty.Create( 
                                    "ItemSource",
                                    typeof (IEnumerable),
                                    typeof (BindablePicker), 
                                    default(IEnumerable), 
                                    BindingMode.TwoWay,
                                    propertyChanged: OnItemsSourceChanged);

        public static BindableProperty SelectedItemBindableProperty =
              BindableProperty.Create(
                                    "SelectedItemBindable",
                                    typeof (object),
                                    typeof (BindablePicker),
                                    default(object),
                                    BindingMode.TwoWay,
                                    propertyChanged: OnSelectedItemChanged);

        public IEnumerable ItemsSource
        {
            get { return (IEnumerable) GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public object SelectedItemBindable
        {
            get { return GetValue(SelectedItemBindableProperty); }
            set { SetValue(SelectedItemBindableProperty, value); }
        }

        public BindablePicker()
        {
            SelectedIndexChanged += OnSelectedIndexChanged;
            base.Title = "Seleccione"; // Here I want a custom title, but this is ignoring me :(
        }

        private static void OnItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
        {
            var picker = bindable as BindablePicker;
            if (picker?.Items == null) return;
            picker.Items.Clear();
            if (newvalue == null) return;
            foreach (var item in ((IEnumerable)newvalue).Cast<object>().Where(item => item != null))
                picker.Items.Add(item.ToString());
        }

        private void OnSelectedIndexChanged(object sender, EventArgs eventArgs)
        {
            if (Items != null && (SelectedIndex < 0 || SelectedIndex > Items.Count - 1))
                SelectedItemBindable = null;
            else if (Items != null) SelectedItemBindable = Items[SelectedIndex];
        }

        private static void OnSelectedItemChanged(BindableObject bindable, object oldvalue, object newvalue)
        {
            var picker = bindable as BindablePicker;
            if (newvalue == null) return;
            if (picker?.Items != null) picker.SelectedIndex = picker.Items.IndexOf(newvalue.ToString());
        }
    }
}

所以问题是:当在XAML上设置Title属性时甚至在C#中设置它总是在Title属性中说“选择项目”我试图在构造函数中设置Picker的Title但是不起作用。

我很感激任何帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

可能只是在控制生命周期的某个地方重写该值的情况。

也许尝试订阅OnAppearing事件并在那里设置标题

答案 1 :(得分:1)

通过Xaml或Code设置Title-Property工作正常。 我刚刚在一个小项目中对此进行了测试。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:control="clr-namespace:AnyNameSpace.Mobile.CustomControls" 
         x:Class="TestApp.BindablePickerTestPage">

  <control:BindablePicker Title="MyTitle"></control:BindablePicker>
</ContentPage>