首页 > 解决方案 > 调用 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="ʕ•ᴥ•ʔ&quot; 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; }
    }
}

标签: c#.netxamarinxamarin.forms

解决方案


ListViewCell必须在其模板中使用类型。将 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


推荐阅读