C# 无法在WPF表单上显示Web API响应
我有一个使用RiotGames API的项目,由于某些原因,我无法在我的WPF表单上显示API响应,我向两个不同的API发出了两个请求,其中只有一个在表单上显示。我不确定到底发生了什么,但任何反馈都很好,不需要是一个完整的解决方案 这是我的MainWindow.xaml.cs文件:C# 无法在WPF表单上显示Web API响应,c#,wpf,C#,Wpf,我有一个使用RiotGames API的项目,由于某些原因,我无法在我的WPF表单上显示API响应,我向两个不同的API发出了两个请求,其中只有一个在表单上显示。我不确定到底发生了什么,但任何反馈都很好,不需要是一个完整的解决方案 这是我的MainWindow.xaml.cs文件: private void Window_Loaded(object sender, RoutedEventArgs e) { initalProfileLoad(); init
private void Window_Loaded(object sender, RoutedEventArgs e)
{
initalProfileLoad();
initalRankedLoad();
}
private async Task initalProfileLoad()
{
await APIConnect.getSummonerByName(Regions.NA, summoner, "Fusion Icee");
this.tbSummonerName.Text = summoner.name;
this.tbSummonerLevel.Text = summoner.summonerLevel.ToString();
}
private async Task initalRankedLoad()
{
var rHandler = new RankHandler();
var rankedSummoner = new LSummonerRanked();
await APIConnect.getSummonerRankBySummonerID(Regions.NA, rankedSummoner, summoner.id);
this.tbRankDivision.Text = rankedSummoner.tier + "" + rankedSummoner.rank;
this.imgRankedIcon.Source = new BitmapImage(new Uri(rHandler.getRankedIcon(rankedSummoner.tier).ToString()));
}
我将这两个连接放在不同的方法中,以便在最大化请求时加载
这是我的APIConnect.cs文件:
public static async Task getSummonerByName(Regions _rg, LSummoner summoner, string summonerName)
{
var summ = new LSummoner();
using (HttpClient client = new HttpClient())
{
Region region = new Region();
client.BaseAddress = new Uri("https://" + region.getRegionConnectionString(_rg));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync($"{SUMMONER_API}{summonerName}?api_key={API_KEY}");
if (response.IsSuccessStatusCode)
{
LSummoner lSummoner = await response.Content.ReadAsAsync<LSummoner>();
summoner.name = lSummoner.name; summoner.profileIconId = lSummoner.profileIconId;
summoner.puuid = lSummoner.puuid; summoner.summonerLevel = lSummoner.summonerLevel;
summoner.revisionDate = lSummoner.revisionDate; summoner.id = lSummoner.id;
summoner.accountId = lSummoner.accountId;
}
else
{
response.EnsureSuccessStatusCode();
}
}
}
/// <summary>
/// Gets the summoner by an Encrypted Summoner ID from the RiotGames API
/// </summary>
/// <param name="_rg"></param>
/// <param name="summoner"></param>
/// <param name="encryptedSummonerID"></param>
/// <returns></returns>
static async Task getSummonerByid(Regions _rg, string encryptedSummonerID)
{
var summ = new LSummoner();
using (HttpClient client = new HttpClient())
{
Region region = new Region();
client.BaseAddress = new Uri("https://" + region.getRegionConnectionString(_rg));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync($"/lol/summoner/v4/summoners/by-account/{encryptedSummonerID}?api_key={API_KEY}");
if (response.IsSuccessStatusCode)
{
LSummoner lSummoner = await response.Content.ReadAsAsync<LSummoner>();
summ.summoners.Add(new LSummoner(lSummoner.profileIconId, lSummoner.name, lSummoner.puuid, lSummoner.summonerLevel, lSummoner.revisionDate,
lSummoner.id, lSummoner.accountId));
}
else
{
response.EnsureSuccessStatusCode();
}
}
}
/// <summary>
/// Get the ranked data of a summoner by the Encrypted SummonerID from the RiotGames API
/// </summary>
/// <param name="_rg"></param>
/// <param name="summoner"></param>
/// <param name="encryptedSummonerID"></param>
/// <returns></returns>
public static async Task getSummonerRankBySummonerID(Regions _rg, LSummonerRanked summonerRanked, string encryptedSummonerID)
{
using (HttpClient client = new HttpClient())
{
Region region = new Region();
client.BaseAddress = new Uri("https://" + region.getRegionConnectionString(_rg));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync($"{RANKED_API}{encryptedSummonerID}?api_key={API_KEY}");
if (response.IsSuccessStatusCode)
{
LSummonerRanked lSummonerRanked = await response.Content.ReadAsAsync<LSummonerRanked>();
summonerRanked.summonerName = lSummonerRanked.summonerName; summonerRanked.tier = lSummonerRanked.tier;
summonerRanked.rank = lSummonerRanked.rank;
}
else
{
response.EnsureSuccessStatusCode();
}
}
}
publicstaticasync任务getcallerbyname(Regions\u rg,LSummoner召唤器,string召唤器名)
{
var summ=新的LSummoner();
使用(HttpClient=new HttpClient())
{
区域=新区域();
client.BaseAddress=新Uri(“https://”+region.getRegionConnectionString(_rg));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“应用程序/json”);
HttpResponseMessage response=wait client.GetAsync($“{caller_-API}{callerName}?API_-key={API_-key}”);
if(响应。IsSuccessStatusCode)
{
LSummoner LSummoner=wait response.Content.ReadAsAsync();
caller.name=lSummoner.name;caller.profileIconId=lSummoner.profileIconId;
caller.puuid=lSummoner.puuid;caller.callerslevel=lSummoner.callerslevel;
caller.revisionDate=lSummoner.revisionDate;caller.id=lSummoner.id;
caller.accountId=lSummoner.accountId;
}
其他的
{
response.EnsureSuccessStatusCode();
}
}
}
///
///通过RiotGames API中加密的召唤器ID获取召唤器
///
///
///
///
///
静态异步任务GetCallerById(区域_rg,字符串加密的召唤器ID)
{
var summ=新的LSummoner();
使用(HttpClient=new HttpClient())
{
区域=新区域();
client.BaseAddress=新Uri(“https://”+region.getRegionConnectionString(_rg));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“应用程序/json”);
HttpResponseMessage response=wait client.GetAsync($“/lol/caller/v4/callers/by account/{encryptedCallerId}?api_key={api_key}”);
if(响应。IsSuccessStatusCode)
{
LSummoner LSummoner=wait response.Content.ReadAsAsync();
Summar.Callerss.Add(新的LSummoner(LSummoner.profileIconId,LSummoner.name,LSummoner.puuid,LSummoner.CallersLevel,LSummoner.revisionDate,
lSummoner.id,lSummoner.accountId));
}
其他的
{
response.EnsureSuccessStatusCode();
}
}
}
///
///通过RiotGames API中加密的召唤ID获取召唤师的排名数据
///
///
///
///
///
公共静态异步任务GetTourgeErrankBySummonerId(区域_rg、LSummonerRanked TourgeRked、字符串加密TourgerId)
{
使用(HttpClient=new HttpClient())
{
区域=新区域();
client.BaseAddress=新Uri(“https://”+region.getRegionConnectionString(_rg));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“应用程序/json”);
HttpResponseMessage response=wait client.GetAsync($“{RANKED_API}{EncryptedCallerID}?API_key={API_key}”);
if(响应。IsSuccessStatusCode)
{
LSummonerRanked LSummonerRanked=wait response.Content.ReadAsAsync();
召唤错误。召唤名称=lSummonerRanked.召唤名称;召唤错误。层=lSummonerRanked.tier;
callowerranke.rank=lsummonerranke.rank;
}
其他的
{
response.EnsureSuccessStatusCode();
}
}
}
LSummoner和LSummonerRanked对象对于公共数据类型都有{get;set;}访问器。
任何帮助都是很好的,不期望有解决方案,只是在正确的方向上的一点。我想试着从这些错误中吸取教训。谢谢大家! 考虑到调用的性质,即混合UI和工作线程,最好使用异步事件处理程序
private async void Window_Loaded(object sender, RoutedEventArgs e) {
//On UI thread here...
//On worker thread here (non-blocking)
await APIConnect.getSummonerByName(Regions.NA, summoner, "Fusion Icee");
//Back on UI Thread
this.tbSummonerName.Text = summoner.name;
this.tbSummonerLevel.Text = summoner.summonerLevel.ToString();
var rHandler = new RankHandler();
var rankedSummoner = new LSummonerRanked();
//On worker thread (non-blocking)
await APIConnect.getSummonerRankBySummonerID(Regions.NA, rankedSummoner, summoner.id);
//Back on UI thread
this.tbRankDivision.Text = rankedSummoner.tier + "" + rankedSummoner.rank;
this.imgRankedIcon.Source = new BitmapImage(new Uri(rHandler.getRankedIcon(rankedSummoner.tier).ToString()));
}
查看评论以了解流程
参考鉴于所进行调用的性质是UI和工作线程的混合,最好使用异步事件处理程序
private async void Window_Loaded(object sender, RoutedEventArgs e) {
//On UI thread here...
//On worker thread here (non-blocking)
await APIConnect.getSummonerByName(Regions.NA, summoner, "Fusion Icee");
//Back on UI Thread
this.tbSummonerName.Text = summoner.name;
this.tbSummonerLevel.Text = summoner.summonerLevel.ToString();
var rHandler = new RankHandler();
var rankedSummoner = new LSummonerRanked();
//On worker thread (non-blocking)
await APIConnect.getSummonerRankBySummonerID(Regions.NA, rankedSummoner, summoner.id);
//Back on UI thread
this.tbRankDivision.Text = rankedSummoner.tier + "" + rankedSummoner.rank;
this.imgRankedIcon.Source = new BitmapImage(new Uri(rHandler.getRankedIcon(rankedSummoner.tier).ToString()));
}
查看评论以了解流程
参考资料这个问题不清楚,很难给你任何具体的回答。请缩小范围并添加问题和错误消息的详细描述(如果存在)。顺便说一句,我注意到您以错误的方式使用HttpClient,详细信息请阅读本文,这里没有错误消息,所以基本上我运行程序时会发生什么,对服务器的第一个GET请求通过并在WPF应用程序上显示结果。但是,当第二个GET请求发出时(getTourgeErrankbySummoneId()),什么也没有发生。它不会抛出一个web Exception或任何东西。这项任务永远不会完成。但是,当我访问web api网页并使用相同的传票ID查询请求时,我得到了一个响应。问题不清楚,很难给你任何具体的响应。请缩小范围并添加问题和错误消息的详细描述(如果存在)。顺便说一句,我注意到您以错误的方式使用HttpClient,有关详细信息,请阅读以下内容