XAML 如何在.NET MAUI中发送和显示获取请求?

sshcrbum  于 2023-03-06  发布在  .NET
关注(0)|答案(2)|浏览(215)

我正在尝试使用. NET MAUI实现一个小应用程序,但在运行时不断出现错误
System.Net.WebException:无法解析主机"http://www.example.com":没有与主机名关联的地址reqres.in": No address associated with hostname'
有时我收到的错误听起来像:
System.InvalidCastException:'指定的转换无效。'
为了显示响应,我使用了ListView控件,最后使用了CollectionView
我使用Nuget软件包管理器安装了Newtonsoft.Json。
代码隐藏文件包含以下代码:

private const string url = "https://reqres.in/api/users";
private HttpClient _Client = new HttpClient();
private ObservableCollection<User> userCollection;

    protected override async void OnAppearing()
    {
        
        base.OnAppearing();

        var httpResponse = await _Client.GetAsync(url);

        if (httpResponse.IsSuccessStatusCode)
        {
            Response responseData = JsonConvert.DeserializeObject<Response>(await httpResponse.Content.ReadAsStringAsync());
            userCollection = new ObservableCollection<User>(responseData.Users);
            User_List.ItemsSource = userCollection;
        }
    }

public class User
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("first_name")]
    public string FirstName { get; set; }

    [JsonProperty("last_name")]
    public string LastName { get; set; }

    [JsonProperty("avatar")]
    public string AvatarURI { get; set; }

    public string FullName
    {
        get { return $"{FirstName} {LastName}"; }
    }
}

public class Response
{
    [JsonProperty("page")]
    public int Page { get; set; }

    [JsonProperty("per_page")]
    public int PerPage { get; set; }

    [JsonProperty("total")]
    public int Total { get; set; }

    [JsonProperty("total_pages")]
    public int TotalPages { get; set; }

    [JsonProperty("data")]
    public ObservableCollection<User> Users { get; set; }
}

XAML文件的内容为:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="TabbedPageTest.TeamMembersPage"
         BackgroundColor="{DynamicResource SecondaryColor}">
  <StackLayout>
    <Label Text="Team Members"/>
    <CollectionView x:Name="User_List" HeightRequest="400" >
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <ImageCell ImageSource="{Binding AvatarURI}" Text="{Binding FullName}" Detail="{Binding Email}"/>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
    <Button Text="Back" Clicked="OnBackButtonClicked" VerticalOptions="CenterAndExpand" />
  </StackLayout>
</ContentPage>
wmvff8tz

wmvff8tz1#

当我使用ListView控件而不是CollectionView时,应用程序运行成功,没有显示错误。
我提到我使用了ListView控件,但我没有正确测试它。

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TabbedPageTest.TeamMembersPage"
             BackgroundColor="{DynamicResource SecondaryColor}">
    <StackLayout>
        <Label Text="Team Members"/>
        <ListView x:Name="User_List" 
                  HeightRequest="400"
                  ItemSelected="OnListItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ImageCell ImageSource="{Binding AvatarURI}" Text="{Binding FullName}" Detail="{Binding Email}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Text="Back" Clicked="OnBackButtonClicked" VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>
w8rqjzmb

w8rqjzmb2#

虽然这是旧的,这里有一个可能的解释,人们搜索类似的问题。
我遇到了同样的问题,所以将httpResponse代码移到了一个新的方法GetData中,并在主页中的InitializeComponent之后调用了该方法。我认为OnAppearing可能存在计时问题。我更改了代码,没有遇到问题。

相关问题