c# - 调用 API 时 Xamarin 应用程序出现问题
问题描述
我是 C# 和整个 .NET 的初学者(只想在开头提及)。所以我正在学习 Xamarin 和 API。
我遇到了从 API ( https://jsonplaceholder.typicode.com/posts ) 获取一些数据以显示在应用程序内部的问题。一旦我执行该应用程序,它就会给我一个错误:
System.InvalidCastException:“指定的演员表无效”。
我也看不到它在哪里失败(错误后,异常窗口为空白 - 就像没有显示它停止的位置)。
using ExerciseApp.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using Xamarin.Forms;
namespace ExerciseApp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
GetPosts();
}
private async void GetPosts()
{
HttpClient client = new HttpClient();
var response = await client.GetStringAsync("https://jsonplaceholder.typicode.com/posts");
var posts = JsonConvert.DeserializeObject<List<Posts>>(response);
PostsListView.ItemsSource = posts;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ExerciseApp.MainPage">
<StackLayout BackgroundColor="White">
<Frame BackgroundColor="#581845" Padding="10" CornerRadius="5" Margin="25">
<Label Text="ʕ•ᴥ•ʔ" HorizontalTextAlignment="Center" TextColor="White" FontSize="30"/>
</Frame>
<Label Text="Welcome to Exercise app with RESTAPI and NHibernate" TextColor="#581845" FontSize="Title" Padding="10" HorizontalTextAlignment="Center"/>
<Label FontSize="20" Padding="20" HorizontalTextAlignment="Center" TextColor="#581845">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Here we will load some data"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
<ListView x:Name="PostsListView">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding id}" TextColor="#581845"></Label>
<Label Text="{Binding title}" TextColor="#581845"></Label>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
namespace ExerciseApp.Models
{
public class Posts
{
public int userId { get; set; }
public int id { get; set; }
public string title { get; set; }
public string body { get; set; }
}
}
解决方案
ListView
Cell
必须在其模板中使用类型。将 a 添加ViewCell
到您的 XAML 将解决此问题
<ListView x:Name="PostsListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding id}" TextColor="#581845"></Label>
<Label Text="{Binding title}" TextColor="#581845"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
或者,您可以使用 a CollectionView
,它允许您创建模板而不受使用 a 的限制Cell
推荐阅读
- python - 如何在python中使用正确的十进制值读取_csv浮点值
- ssrs-2008 - 从我的 SSRS 报告中每个组的总数计算总计
- ios - 键盘扩展为 UIKeyboardTypeNumberPad 返回 needsInputModeSwitchKey false
- pymatgen - 相对于晶轴的磁矩?
- html - 如何在某个位置显示带有使用(three.js)创建的3D对象的窗口?
- traefik - 与 docker compose 一起使用时覆盖默认的 traefik 前端规则
- html - CSS 引导程序:list-group-item hover+selected
- javascript - 如果我为模态添加数据切换,Linkbutton OnClick 将不会触发
- sql - 像 postgresql 数据问题中的运算符条件
- java - 如何在 ARCore Sceneform 中为节点创建动画(更改缩放、旋转或位置)?