API结果未显示C#UWP
大家好, 我正在尝试使用API来显示当前比特币的价格。 API返回结果和所有内容,但它不会在UWP应用程序上显示 奇怪的是,它确实显示了一次结果,但此后再也没有显示结果API结果未显示C#UWP,c#,api,uwp,C#,Api,Uwp,大家好, 我正在尝试使用API来显示当前比特币的价格。 API返回结果和所有内容,但它不会在UWP应用程序上显示 奇怪的是,它确实显示了一次结果,但此后再也没有显示结果 是的,API非常快 这是我的主页代码: public sealed partial class MainPage : Page, INotifyPropertyChanged { public string Price { get; set; } private DispatcherTimer _timer
- 是的,API非常快
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public string Price { get; set; }
private DispatcherTimer _timer;
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
this.InitializeComponent();
this._timer = new DispatcherTimer();
this._timer.Interval = TimeSpan.FromSeconds(20);
this._timer.Tick += OnUpdate;
this._timer.Start();
}
private async void OnUpdate(object sender, object e)
{
this.Price = (await API.GetData(1))[0]["price_eur"];
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Price"));
}
}
class API
{
public static async Task<List<Dictionary<string, string>>> GetData(int limit)
{
var url = "https://api.coinmarketcap.com/v1/ticker/?convert=EUR&limit=" + limit;
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(result);
}
else
{
return null;
}
}
}
}
<RelativePanel>
<Rectangle x:Name="rctOrange" Fill="Orange" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True" Stretch="UniformToFill"/>
<TextBlock x:Name="tbPrice" FontSize="80" Text="{x:Bind Price, Mode=OneWay}" RelativePanel.AlignVerticalCenterWith="rctOrange" RelativePanel.AlignHorizontalCenterWith="rctOrange"/>
</RelativePanel>
这是我的API类代码:
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public string Price { get; set; }
private DispatcherTimer _timer;
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
this.InitializeComponent();
this._timer = new DispatcherTimer();
this._timer.Interval = TimeSpan.FromSeconds(20);
this._timer.Tick += OnUpdate;
this._timer.Start();
}
private async void OnUpdate(object sender, object e)
{
this.Price = (await API.GetData(1))[0]["price_eur"];
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Price"));
}
}
class API
{
public static async Task<List<Dictionary<string, string>>> GetData(int limit)
{
var url = "https://api.coinmarketcap.com/v1/ticker/?convert=EUR&limit=" + limit;
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(result);
}
else
{
return null;
}
}
}
}
<RelativePanel>
<Rectangle x:Name="rctOrange" Fill="Orange" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True" Stretch="UniformToFill"/>
<TextBlock x:Name="tbPrice" FontSize="80" Text="{x:Bind Price, Mode=OneWay}" RelativePanel.AlignVerticalCenterWith="rctOrange" RelativePanel.AlignHorizontalCenterWith="rctOrange"/>
</RelativePanel>
类API
{
公共静态异步任务GetData(整数限制)
{
变量url=”https://api.coinmarketcap.com/v1/ticker/?convert=EUR&limit=“+限制;
使用(var client=new HttpClient())
{
var response=wait client.GetAsync(url);
if(响应。IsSuccessStatusCode)
{
var result=await response.Content.ReadAsStringAsync();
返回JsonConvert.DeserializeObject(结果);
}
其他的
{
返回null;
}
}
}
}
这是我的主页xaml代码:
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public string Price { get; set; }
private DispatcherTimer _timer;
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
this.InitializeComponent();
this._timer = new DispatcherTimer();
this._timer.Interval = TimeSpan.FromSeconds(20);
this._timer.Tick += OnUpdate;
this._timer.Start();
}
private async void OnUpdate(object sender, object e)
{
this.Price = (await API.GetData(1))[0]["price_eur"];
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Price"));
}
}
class API
{
public static async Task<List<Dictionary<string, string>>> GetData(int limit)
{
var url = "https://api.coinmarketcap.com/v1/ticker/?convert=EUR&limit=" + limit;
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(result);
}
else
{
return null;
}
}
}
}
<RelativePanel>
<Rectangle x:Name="rctOrange" Fill="Orange" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True" Stretch="UniformToFill"/>
<TextBlock x:Name="tbPrice" FontSize="80" Text="{x:Bind Price, Mode=OneWay}" RelativePanel.AlignVerticalCenterWith="rctOrange" RelativePanel.AlignHorizontalCenterWith="rctOrange"/>
</RelativePanel>
我希望你们能找到问题所在,因为我快疯了
提前感谢您不应该在页面上实现INotifyPropertyChanged。您应该创建一个实现INotifyPropertyChanged的ViewModel
private async void OnUpdate(object sender, object e)
{
this.Price.Value = (await API.GetData(1))[0]["price_eur"];
}
class PriceModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _value;
public string Value
{
get { return _value; }
set
{
_value = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
}
}
}
如果您不向我们提供有关出现问题的信息,我们将无法帮助您。简单地问“为什么这不起作用”并不是这个论坛的主题。是否有任何错误,到目前为止需要调试什么?@Glubus没有错误,程序正常启动。API调用返回正确的信息,但唯一的问题是它不会在应用程序中放置“price_eur”。我理解,但我试图向您传达,我们需要更多的信息来帮助您。您要求我们为您找到一个问题,以便您/我们能够解决它。你需要提出一个具体的问题,你需要帮助,而不是离开这个论坛的工作。谢谢,这个答案帮了我很多!