【发布时间】:2020-10-26 21:30:22
【问题描述】:
我对编程真的很陌生,所以如果我使用了不正确的术语,我深表歉意。我正在尝试在 Blazor 应用程序中使用 Riot 的 API,但是在单击使用 API 设置值的按钮后,我在页面上显示的变量没有更新为它们所具有的值。可能是因为我实例化对象的方式。
我试图让 summoner.Name 和 summoner.Level 显示名称和级别,但它们在页面上是空的,即使在单击按钮之后也是如此.我已经为这两个项目添加了一个手表,并确认它们正在返回代码中的值,但不知道为什么它们没有在页面上更新。是因为 Summoner caller = new Summoner(); 这一行吗?我尝试将其设置为 null,但这会导致错误。
代码如下:
@using Newtonsoft.Json
@page "/"
@inject HttpClient http;
<h1>API Calling</h1>
<button class="btn btn-success" @onclick="@GetSummonerTask">Get Summoner</button>
<br>
<p>
NAME: @summoner.Name
<br>
LEVEL: @summoner.Level
</p>
@code{
Summoner summoner = new Summoner();
private async Task GetSummonerTask()
{
SummonerInformation summonerInformation = new SummonerInformation();
Summoner summoner = await summonerInformation.GetSummoner();
}
public class SummonerInformation
{
HttpClient client = new HttpClient();
public async Task<Summoner> GetSummoner()
{
var summonerName = await client.GetAsync(new Uri("https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/weeatrocks?api_key={I have my API key here but taking out for obvious reasons}"));
var JSON = await summonerName.Content.ReadAsStringAsync();
Summoner summoner = JsonConvert.DeserializeObject<Summoner>(JSON);
return summoner;
}
}
public class Summoner
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "summonerLevel")]
public string Level { get; set; }
}
}
【问题讨论】:
-
任何错误消息 javascript 或其他
-
控制台中没有错误信息