我知道这是一个常见问题,但我找不到答案。我正在尝试 Microsoft Emotion API(我在这里使用了通用 key 来提出问题),它不断给我错误,“WindowsFormsApp2
是一个命名空间,但被用作一种类型”,即使在我更改了命名空间。我将命名空间更改为更合适的标题,但我仍然收到构建错误 WindowsFormsApp2
被不恰本地用作类型,尽管事实上它在代码中没有任何地方。我不知道我在其他什么地方使用它会导致这个问题。
这是我的代码:
using System.Windows.Forms;
using System.IO;
using System.Net.Http.Headers;
using System.Net.Http;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
{
textBox1.Text = ("Enter the path to a JPEG image file:");
openFileDialog1.ShowDialog();
string imageFilePath = openFileDialog1.FileName;
MakeRequest(imageFilePath);
textBox1.Text = ("\n\n\nWait for the result below, then hit ENTER to exit...\n\n\n");
}
byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
async void MakeRequest(string imageFilePath)
{
var client = new HttpClient();
// Request headers - replace this example key with your valid key.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "13hc77781f7e4b19b5fcdd72a8df7156");
// NOTE: You must use the same region in your REST call as you used to obtain your subscription keys.
// For example, if you obtained your subscription keys from westcentralus, replace "westus" in the
// URI below with "westcentralus".
string uri = "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize?";
HttpResponseMessage response;
string responseContent;
// Request body. Try this sample with a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (var content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);
responseContent = response.Content.ReadAsStringAsync().Result;
}
//A peak at the JSON response.
textBox1.Text = (responseContent);
}
}
}
}
最佳答案
我不喜欢 InitializeComponent()
后面的分号以你的形式。
InitializeComponent(); <--- !!!
{
textBox1.Text = ("Enter the path to a JPEG image file:");
...
}
关于c# - "FormsApp is a namespace but used like type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46516494/