using System.Net.Http.Json; namespace MauiApi; public partial class MainPage : ContentPage { private static readonly HttpClient Client = new() { Timeout = TimeSpan.FromSeconds(15) }; public MainPage() => InitializeComponent(); private async void OnLoadClicked(object sender, EventArgs e) { LoadButton.IsEnabled = false; StatusLabel.Text = "Loading..."; try { const string url = "https://jsonplaceholder.typicode.com/posts?_limit=5"; var posts = await Client.GetFromJsonAsync>(url); PostsView.ItemsSource = posts ?? []; StatusLabel.Text = $"Loaded {posts?.Count ?? 0} posts."; } catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException) { PostsView.ItemsSource = null; StatusLabel.Text = "Request failed. Check Internet access and try again."; } finally { LoadButton.IsEnabled = true; } } }