简体   繁体   English

指定的转换在 Xamarin Forms 中无效

[英]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[] {... } ... } 的缩写,它使用{}中的初始值创建一个新数组。

See array documentation . 请参阅阵列文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的演员表无效 - Xamarin Forms - Specified cast is not valid - Xamarin Forms 指定的强制转换无效(xamarin 形式) - Specified cast is not valid (xamarin forms) Xamarin.Forms 绑定指定的强制转换无效 - Xamarin.Forms Binding Specified cast is not valid Xamarin.Forms:使用 Picker 时错误指定的强制转换无效 - Xamarin.Forms: Error Specified cast is not valid when using Picker Xamarin 形式:System.InvalidCastException:“指定的强制转换无效。” - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid.' Xamarin forms: System.InvalidCastException: '指定的转换无效 - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid Xamarin 表单与 Firebase。 数据检索抛出 System.InvalidCastException: &#39;指定的转换无效。&#39; - Xamarin Forms with Firebase. Data retrival throwing System.InvalidCastException: 'Specified cast is not valid.' 为什么从 sqlite db 中选择会显示错误“指定的强制转换无效”。 在 Xamarin 表单中? - Why does select from sqlite db shows error 'Specified cast is not valid.' in Xamarin Forms? 在 Xamarin Forms 中获取 SelectedItem 值时,选择器错误“指定的转换无效” - Picker error “Specified cast is not valid” when getting the SelectedItem value in Xamarin Forms Xamarin Forms:在可绑定的内部添加全局值时指定的转换无效 object:列表视图 - Xamarin Forms: Specified cast is not valid while adding a global value inside a bindable object: list view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM