简体   繁体   English

API 结果未显示 C# UWP

[英]API result not showing C# UWP

Hello everyone,大家好,

I'm trying to use an API to show the current Bitcoin price.我正在尝试使用 API 来显示当前的比特币价格。 The API returns the result and everything but it just won't show it on the UWP application. API 返回结果和所有内容,但不会在 UWP 应用程序上显示它。

The strange thing is that it did show the result one time but after that it never showed the result again.奇怪的是,它确实显示了一次结果,但之后就再也没有显示过结果。

  • Yes the API is really fast.是的,API 真的很快。

Here is my MainPage code:这是我的主页代码:

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"));
    }
}

Here is my API class code:这是我的 API 类代码:

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;
            }
        }
    }
}

Here is my MainPage xaml code:这是我的 MainPage xaml 代码:

<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>

I hope you guys can find the problem because I'm getting crazy.我希望你们能找到问题,因为我快疯了。

Thanks in advance!提前致谢!

You shouldn't implement INotifyPropertyChanged on your page.您不应该在您的页面上实现 INotifyPropertyChanged。 You should create an ViewModel that implements 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"));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM