Как вывести данные на экран без использования кнопки?
MainPage.xaml
<StackLayout >
<Button Text="ccccccc" />
<Label FontSize="Medium" Text="Value" />
<Label FontSize="Medium" FontAttributes="Bold" Text="{Binding Value}" />
<Label FontSize="Medium" Text="Detail" />
<Label FontSize="Medium" FontAttributes="Bold" Text="{Binding Detail}" />
</StackLayout>
RateInfo.cs
public class Confirmed
{
public int value { get; set; }
public string detail { get; set; }
}
RateViewModel.cs
using json_test;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.ComponentModel;
using System.Net.Http;
using System.Windows.Input;
using Xamarin.Forms;
namespace RestApp
{
public class RateViewModel : INotifyPropertyChanged
{
private int value;
private string detail;
public int Value
{
get { return value; }
private set
{
this.value = value;
OnPropertyChanged("Value");
}
}
public string Detail
{
get { return detail; }
private set
{
detail = value;
OnPropertyChanged("Detail");
}
}
public ICommand LoadDataCommand { protected set; get; }
public RateViewModel()
{
this.LoadDataCommand = new Command(LoadData);
}
private async void LoadData()
{
string url = "";
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
var response = await client.GetAsync(client.BaseAddress);
response.EnsureSuccessStatusCode(); // выброс исключения, если произошла ошибка
// десериализация ответа в формате json
var content = await response.Content.ReadAsStringAsync();
JObject o = JObject.Parse(content);
var str = o.SelectToken(@"$.confirmed");
var rateInfo = JsonConvert.DeserializeObject<Confirmed>(str.ToString());
this.Value = rateInfo.value;
this.Detail = rateInfo.detail;
}
catch (Exception )
{ }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}