[英]Specified cast is not valid in Xamarin Forms
Does anyone know what happens when getting an error like Specified cast is not valid?有谁知道当出现像 Specified cast is not valid 这样的错误时会发生什么? I commented on the line where the error happens我评论了错误发生的那一行
private async void GetEmployee()
{
var _token = await GetAccessToken();
using (var _client = new HttpClient())
{
var _uri = "domain here";
_client.BaseAddress = new Uri(_uri);
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token);
var _response = await _client.GetAsync("endpoint here'");
var Emp = JsonConvert.DeserializeObject<Employee>(await _response.Content.ReadAsStringAsync());
Employee = new ObservableCollection<Employee>((IEnumerable<Employee>)Emp); //Im having error on this line
}
}
ObservableCollection<Employee> _employee;
public ObservableCollection<Employee> Employee
{
get
{
return _employee;
}
set
{
_employee = value;
OnPropertyChanged();
}
}
How about changing this:如何改变这个:
Employee = new ObservableCollection<Employee>((IEnumerable<Employee>)Emp);
to this:对此:
Employee = new ObservableCollection<Employee>(new[] {Emp});
See .NET Fiddle example .请参阅 .NET 小提琴示例。
See ObservableCollection documentation . 请参阅 ObservableCollection 文档。
The new[] {... }
is short for new Employee[] {... }
and creates a new array with the initial value(s) inside the {
and }
. new[] {... }
是new Employee[] {... }
... } 的缩写,它使用{
和}
中的初始值创建一个新数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.