使用Xamarin.forms消耗RESTful API

时间:2019-03-19 01:35:34

标签: c# rest api xamarin.forms

Xamarin.forms的新功能。

以下代码应生成游戏列表并将其绑定到列表视图,但不是。我正在使用改装库。 Postman中的URL将按预期返回JSON列表。我应该从哪里开始?

ISteamService.CS

using System.Collections.Generic;
using System.Threading.Tasks;
using Refit;

namespace PatchNotes
{

[Headers("Content-Type: application/json")]
public interface ISteamService
{
    [Get("/IPlayerService/GetOwnedGames/v1/?key=XXXXC&include_appinfo=1&steamid=XXXX")]
    Task<string> GetGames();
}

}

MainPage.xaml.cs

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xamarin.Forms;
using Refit;

namespace PatchNotes
{
    public partial class MainPage : ContentPage
    {
    public MainPage()
    {
        InitializeComponent();
    }

    async void OnGetGamesClicked(object sender, System.EventArgs e)
    {
        var apiResponse = RestService.For<ISteamService>("https://api.steampowered.com");
        var games = await apiResponse.GetGames();

        GamesList.ItemsSource = games;

    }
}
}

MainPage.Xaml

<?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="PatchNotes.MainPage">
<ContentPage.Content>
    <StackLayout Padding="40">
         <Button Text="Get Games" Clicked="OnGetGamesClicked" 
BackgroundColor="Black" TextColor="White" 
HorizontalOptions="FillAndExpand"/>

          <ListView x:Name="GamesList">
            <ListView.ItemTemplate>
              <DataTemplate>
                <TextCell Text="{Binding Name}" />
              </DataTemplate>
            </ListView.ItemTemplate>
           </ListView>
    </StackLayout>
</ContentPage.Content>

1 个答案:

答案 0 :(得分:1)

根据this documentation GetOwnedGames将返回具有以下结构的JSON(如Jason正确猜测的那样)

{
    "game_count": <number of games>,
    "games": [ 
        {
            "appid": "...",
            "name": "...",
            "playtime_2weeks": ,
            "playtime_forever": ,
            "img_icon_url": "",
            "img_logo_url": "",
            "has_community_visible_stats": "" 
        }, ...]
}

您不能简单地将此字符串分配给ItemsSource并期望 Xamarin.Forms 为您解决,您将必须注意如何对其进行反序列化。

您将必须编写一个用于反序列化JSON字符串的类。 Refit 将进行反序列化,但是无论如何您都需要一个类来反序列化:

public class GamesList
{
    [JsonProperty("game_count")]
    public int GameCount { get; set; }

    [JsonProperty("games")]
    public List<Game> Games { get; set; }
}

public class Game
{
    [JsonProperty("appid")]
    public string AppId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("playtime_2weeks")]
    public int PlayedMinutesInTheLastTwoWeeks { get; set; }

    // And so on. Depending on what you need.
}

(请参见JsonProperty上的JSON.Net documentation

您现在可以重新定义ISteamService

[Headers("Content-Type: application/json")]
public interface ISteamService
{
    [Get("/IPlayerService/GetOwnedGames/v1/?key=XXXXC&include_appinfo=1&steamid=XXXX")]
    Task<GamesList> GetGames();
}

并从OnGetGamesCicked

var apiResponse = RestService.For<ISteamService>("https://api.steampowered.com");
var gamesList = await apiResponse.GetGames();

GamesList.ItemsSource = gamesList.Games;