Xamarin选择器未显示列表中的日期 [英] Xamarin picker not showing the date from the list

查看:100
本文介绍了Xamarin选择器未显示列表中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

    <ContentPage.BindingContext>
        <local:PisterosViewModel/>
    </ContentPage.BindingContext>

<Picker x:Name="pck_Pisteros"
                        ItemDisplayBinding="{Binding PisteroN}"
                        ItemsSource="{Binding PisterosLista}"
                        SelectedItem="{Binding PisterosLista}"
                        Title="Seleccione el usuario..."/>

然后是我的模特:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Text;

namespace ServLottery.Models
{
    public class Pisteros
    {
        public string PisteroID { get; set; }
        public string PisteroN { get; set; }

    }

}

和视图模型:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace ServLottery.Models
{
    public class PisterosViewModel
    {
        public IList<Pisteros> PisterosLista { get; set; }

        public PisterosViewModel()
        {
            try
            {
                PisterosLista = new ObservableCollection<Pisteros>();
                GetPisteros();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private async void GetPisteros()
        {
            try
            {
                RestClient client = new RestClient();
                var pist = await client.Get<Models.Pisteros>("https://servicentroapi.azurewebsites.net/api/Pisteros");
                if (pist != null)
                {
                    PisterosLista = pist;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

    }
}

我在var pist中设置了一个断点,它确实有值,然后Pisteros列表似乎也得到了值,并且这在页面加载时执行,所以我不明白出了什么问题,但是选择器从不显示选项.

I set an breakpoint in var pist and it does have the values, then Pisteros list seems to get the values too, and this is executed when the page load, so I don't understand what's the problem, but the picker never shows the options.

推荐答案

欢迎使用SO!

似乎 Xaml 中的BindingContext无法处理动态数据,例如来自Web服务器的API数据.

It seems like BindingContext in Xaml can not deal with the dynamical data ,such API data from web server .

有一个解决方法,可以通过使用ContentPage中的编码动态绑定ItemSource,也可以参考

There is a workaround binding ItemSource dynamically by using coding in ContentPage , also can refer to this officail sample .

因此,请在 Page.Xaml.cs 中添加代码,如下所示:

Therefore , adding code in Page.Xaml.cs as follow :

protected override async void OnAppearing()
{
    pck_Pisteros.ItemsSource = await GetTasksAsync();
    base.OnAppearing();
}

private async Task<List<Pisteros>> GetTasksAsync()
{
    List<Pisteros> PisterosLista = new List<Pisteros>();
    HttpClient client = new HttpClient();
    Uri uri = new Uri(string.Format("https://servicentroapi.azurewebsites.net/api/Pisteros", string.Empty));
    HttpResponseMessage response = await client.GetAsync(uri);
    if (response.IsSuccessStatusCode)
    {
        string content = await response.Content.ReadAsStringAsync();
        PisterosLista = JsonConvert.DeserializeObject<List<Pisteros>>(content);
        Console.WriteLine("content :: " + content);
        Console.WriteLine("Data :: " + PisterosLista);
    }

    return PisterosLista;
}

现在它将显示:

这篇关于Xamarin选择器未显示列表中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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