Вот мой код - моя модель представления: < /p>
Код: Выделить всё
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Meetoplatform.API;
using System.Text.Json;
using static Meetoplatform.API.WBSMeeto;
namespace Meetoplatform.ViewModel
{
public partial class VMMeetman : ObservableObject
{
// Declared an HttpClient instance?
private static readonly WBSMeeto? _apiService;
// Trying to get a list of users?
private List _meetmans;
// Creating instances of Meetman entity attributes for further binding?
[ObservableProperty]
public string _meetmanId;
[ObservableProperty]
public string _iname;
[RelayCommand]
public static async Task GetDataAsync()
{
// Trying to connect to HttpClietn and retrieve data from the API?
var meetman = await _apiService.GetMeetmanAsync();
// Trying to serialize the received data?
string json = JsonSerializer.Serialize(meetman, options: new JsonSerializerOptions { WriteIndented = true });
// Am I doing it right, and what should I do next?
}
}
}
< /code>
Мой класс для подключения к API: < /p>
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Meetoplatform.API
{
public class WBSMeeto
{
public class Meetman
{
#region Entity Attributes
public int MeetmanId { get; set; }
public string Iname { get; set; }
public string Sname { get; set; }
public string Tname { get; set; }
public string Phnumb { get; set; }
public string Datebirth { get; set; }
public string Location { get; set; }
public string Email { get; set; }
public string IsMan { get; set; }
public string IsWooman { get; set; }
#endregion
};
private readonly HttpClient _httpClient;
private readonly JsonSerializerOptions _jsonOptions;
public WBSMeeto()
{
_httpClient = new HttpClient()
{
BaseAddress = new Uri("http://my connection string")
};
_jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true
};
}
public async Task GetMeetmanAsync()
{
var response = await _httpClient.GetAsync(_httpClient.BaseAddress);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize(json, _jsonOptions);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... rom-an-api
Мобильная версия