Xamarin中的Picker功能不显示项目

时间:2018-03-13 08:56:42

标签: c# xamarin xamarin.forms xamarin.android dropdown

这是我的代码。

List<DriverStatusViewModel> driverStatus = PopulateDriverStatus();
            var selectedStatus = driverStatus.FirstOrDefault();

            var picker = new Picker { Title = "Select a status" };
            picker.SetBinding(Picker.ItemsSourceProperty, "driverStatus");
            picker.SetBinding(Picker.SelectedItemProperty, "selectedStatus");
            picker.ItemDisplayBinding = new Binding("Status");

填充列表

private List<DriverStatusViewModel> PopulateDriverStatus()
        {
            var driverStatus = new List<DriverStatusViewModel>();
            var stat1 = new DriverStatusViewModel();
            stat1.EnumId = 1;
            stat1.Status = "At Lunch";
            driverStatus.Add(stat1);
            var stat2 = new DriverStatusViewModel();
            stat2.EnumId = 2;
            stat2.Status = "Break";
            driverStatus.Add(stat2);
            var stat3 = new DriverStatusViewModel();
            stat3.EnumId = 3;
            stat3.Status = "Delivering";
            driverStatus.Add(stat3);
            return driverStatus;
        }

Xaml代码显示

<Picker Title="Select a status"
                                ItemsSource="{Binding driverStatus}"
                                TextColor="Black"
                                BackgroundColor="Red"
                                ItemDisplayBinding="{Binding Name}"
                                SelectedItem="{Binding selectedStatus}" />

下图是在模拟器中运行时的样子 enter image description here

我是Xamarin的新手,请帮助我,并提前感谢你。

3 个答案:

答案 0 :(得分:1)

问题是,当您在代码中设置Binding时,string无法指向&#34;指向&#34;到一个局部变量。绑定在运行时进行评估,因此视图模型中必须有一个名为driverStatusselectedDriver的属性。

因此,不要将其设置为局部变量,而是创建两个属性DriverStatusSelectedDriver并绑定它们。此外,您可能希望使用INotifyPropertyChanged,以便在属性值更改时通知UI。

答案 1 :(得分:1)

在Xaml中,添加x:Name="pic"

<Picker Title="Select a monkey" 
        TextColor="Black"
        x:Name="pic"
        BackgroundColor="Red"
        ItemDisplayBinding="{Binding Name}"
        SelectedItem="{Binding selectedStatus}"/>

在您的页面中添加:

pic.ItemsSource = driverStatus;

答案 2 :(得分:0)

这就是我为解决Picker所做的工作。

//视图模型

dfs.client.use.datanode.hostname 
dfs.datanode.use.datanode.hostname

//模型

public class DriverStatusViewModel : INotifyPropertyChanged
    {
        List<DriverStatus> statusList;
        public List<DriverStatus> StatusList
        {
            get { return statusList; }
            set
            {
                if (statusList != value)
                {
                    statusList = value;
                    OnPropertyChanged();
                }
            }
        }
        public string Title { get; set; }
        DriverStatus selectedStatus;
        public DriverStatus SelectedStatus
        {
            get { return selectedStatus; }
            set
            {
                if (selectedStatus != value)
                {
                    selectedStatus = value;
                    OnPropertyChanged();
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

// cs file

public class DriverStatus
    {
        public int E_Id { get; set; }
        public string E_Name { get; set; }
    }

//为DriverStatus添加静态数据

BindingContext = new DriverStatusViewModel
            {
                StatusList = PopulateDriverStatus()
            };

// XAML文件

private List<DriverStatus> PopulateDriverStatus()
        {
            var driverStatus = new List<DriverStatus>();
            var stat1 = new DriverStatus();
            stat1.E_Id = 1;
            stat1.E_Name = "At Lunch";
            driverStatus.Add(stat1);
            var stat2 = new DriverStatus();
            stat2.E_Id = 2;
            stat2.E_Name = "Break";
            driverStatus.Add(stat2);
            var stat3 = new DriverStatus();
            stat3.E_Id = 3;
            stat3.E_Name = "Delivering";
            driverStatus.Add(stat3);
            return driverStatus;
        }

特别感谢这个回答Xamarin Forms Picker Binding我跟着他做了什么。