【问题标题】:Problem with Xamarin app while calling an API调用 API 时 Xamarin 应用程序出现问题
【发布时间】:2021-06-18 10:13:35
【问题描述】:

我是 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; }
    }
}

【问题讨论】:

  • 如果异常没有显示它发生的位置,请检查堆栈跟踪,如果没有帮助,那么您需要在调试器中设置断点并单步执行每一行代码,直到你遇到了异常
  • @Jason 我现在经历了所有的断点 - 它并没有在某个点停止。它只是在执行所有内容后显示(所以在函数之后)。我认为这就是为什么它没有在错误处理程序中显示任何信息的原因。
  • GetPosts 是异步的,因此请尝试使用 await 而不是构造函数从 OnAppearing 调用它
  • @Jason 抱歉,但我对它很陌生,我不知道该怎么做。

标签: c# .net xamarin xamarin.forms


【解决方案1】:

ListView 必须在其模板中使用 Cell 类型。在 XAML 中添加 ViewCell 将解决此问题

    <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> 

或者,您可以使用CollectionView,它允许您创建模板而不受使用Cell的限制

【讨论】:

  • 你是老板!奇迹般有效。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 2022-07-14
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
相关资源
最近更新 更多