Xamarin 选择器未显示列表中的日期 [英] Xamarin picker not showing the date from the list
问题描述
我有以下代码:
<local:PisterosViewModel/></ContentPage.BindingContext><Picker x:Name="pck_Pisteros";ItemDisplayBinding="{Binding PisteroN}";ItemsSource="{Binding PisterosLista}";SelectedItem="{Binding PisterosLista}";标题=Seleccione el usuario..."/>
然后是我的模型:
使用系统;使用 System.Collections.Generic;使用 System.Collections.ObjectModel;使用 System.Diagnostics.Contracts;使用 System.Text;命名空间 ServLottery.Models{公开课 Pisteros{公共字符串 PisteroID { 获取;放;}公共字符串 PisteroN { 获取;放;}}}
和视图模型:
使用系统;使用 System.Collections.Generic;使用 System.Collections.ObjectModel;使用 System.Text;命名空间 ServLottery.Models{公共类 PisterosViewModel{公共 IList<Pisteros>PisterosLista { 得到;放;}公共 PisterosViewModel(){尝试{PisterosLista = new ObservableCollection<Pisteros>();GetPisteros();}捕获(异常前){扔前;}}私有异步 void GetPisteros(){尝试{RestClient 客户端 = new RestClient();var pist = await client.Get(https://servicentroapi.azurewebsites.net/api/Pisteros");if (pist != null){PisterosLista = pist;}}捕获(异常前){扔前;}}}}
我在 var pist 中设置了一个断点,它确实有值,然后 Pisteros 列表似乎也获得了值,并且在页面加载时执行,所以我不明白是什么问题,但选择器从来没有显示选项.
欢迎来到 SO!
似乎Xaml中的BindingContext
无法处理动态数据,例如来自Web服务器的API数据.
在 ContentPage 中使用编码动态绑定 ItemSource
有一个解决方法,也可以参考
I have the following code:
<ContentPage.BindingContext>
<local:PisterosViewModel/>
</ContentPage.BindingContext>
<Picker x:Name="pck_Pisteros"
ItemDisplayBinding="{Binding PisteroN}"
ItemsSource="{Binding PisterosLista}"
SelectedItem="{Binding PisterosLista}"
Title="Seleccione el usuario..."/>
Then my Model:
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; }
}
}
and the view model:
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;
}
}
}
}
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.
Welcome to SO !
It seems like BindingContext
in Xaml can not deal with the dynamical data ,such API data from web server .
There is a workaround binding ItemSource
dynamically by using coding in ContentPage , also can refer to this officail sample .
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;
}
Now it will show:
这篇关于Xamarin 选择器未显示列表中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!