C# 更新标签文本仅使用给定文本的一部分

C# 更新标签文本仅使用给定文本的一部分,c#,winforms,C#,Winforms,我是C#新手,尝试在API发出http请求后更新标签文本,但没有成功。 我是这样做的: private void buttonGenerateKey_Click(object sender, EventArgs e) { var apiKey = getApiKey(); Console.WriteLine("Your API Key: " + apiKey); //-> Your API Key: Knh4dH4d8rtWfgXr5 labelApiKeyTex

我是C#新手,尝试在API发出http请求后更新标签文本,但没有成功。 我是这样做的:

 private void buttonGenerateKey_Click(object sender, EventArgs e)
{
    var apiKey = getApiKey();
    Console.WriteLine("Your API Key: " + apiKey); //-> Your API Key: Knh4dH4d8rtWfgXr5
    labelApiKeyText.Text = "Your API Key: " + apiKey; //-> Your API Key:
    Console.WriteLine("Label Content: " + labelApiKeyText.Text); //-> Your API Key: Knh4dH4d8rtWfgXr5
    labelApiKeyText.ForeColor = Color.ForestGreen;
    labelApiKeyText.Refresh();
}

// --- Get an API Key
private string getApiKey()
{
    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("http://localhost:61128/StationService.svc/");
    HttpResponseMessage response = httpClient.GetAsync("key").Result;
    var ApiKeyString = response.Content.ReadAsStringAsync();
    var ApiKey = JsonConvert.DeserializeObject<string>(ApiKeyString.Result);
    return ApiKey;
}
private void按钮GenerateKey\u单击(对象发送者,事件参数e)
{
var apiKey=getApiKey();
WriteLine(“您的API密钥:+apiKey”);/->您的API密钥:Knh4dH4d8rtWfgXr5
labelApiKeyText.Text=“您的API密钥:”+apiKey;/>您的API密钥:
Console.WriteLine(“标签内容:+labelApiKeyText.Text);//->您的API键:Knh4dH4d8rtWfgXr5
labelApiKeyText.ForeColor=Color.ForestGreen;
labelApiKeyText.Refresh();
}
//---获取API密钥
私有字符串getApiKey()
{
HttpClient HttpClient=新HttpClient();
httpClient.BaseAddress=新Uri(“http://localhost:61128/StationService.svc/");
HttpResponseMessage response=httpClient.GetAsync(“key”).Result;
var ApiKeyString=response.Content.ReadAsStringAsync();
var ApiKey=JsonConvert.DeserializeObject(ApiKeyString.Result);
返回ApiKey;
}
我可以在控制台中看到正确的文本,但标签仅显示“您的API密钥:”


即使在尝试使用后台工作人员后,我也没有成功。我做错什么了吗?

将代码更改为以下内容,看看是否有帮助

private async string getApiKey()
{
    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("http://localhost:61128/StationService.svc/");
    HttpResponseMessage response = await httpClient.GetAsync("key");
    if(response==null)
    {
        // something went wrong
    }
    var ApiKeyString = await response.Content.ReadAsStringAsync();
    var ApiKey = JsonConvert.DeserializeObject<string>(ApiKeyString);
    return ApiKey;
}
private异步字符串getApiKey()
{
HttpClient HttpClient=新HttpClient();
httpClient.BaseAddress=新Uri(“http://localhost:61128/StationService.svc/");
HttpResponseMessage response=等待httpClient.GetAsync(“key”);
如果(响应==null)
{
//出了点问题
}
var ApiKeyString=await response.Content.ReadAsStringAsync();
var ApiKey=JsonConvert.DeserializeObject(ApiKeyString);
返回ApiKey;
}

您确定已命中此处理程序吗?单击事件,我的意思是?@st_stefanov Yes,
Console.WriteLine(“您的API键:+apiKey”)显示在控制台中。请在此Console.WriteLine之后再添加一行,并在此处写入labelApiKey.Textl的当前值。告诉我们它是什么。
GetAsync(“key”)
不会返回任何内容,您需要将uri传递给http客户端。使用
async/await
阻塞。Result
是的,很好的观点是JSteward,但是他说在控制台中,值很好,所以我们假设ApiKey变量有一个值。