"FormsApp is a namespace but used like type"
"FormsApp is a namespace but used like type"
我知道这是一个常见问题,但我找不到答案。我正在尝试 Microsoft Emotion API(我在这里使用通用键来提出问题),它一直给我错误,“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:");
...
}
我发现了问题,实际上不在提供的代码中。 IDE 认为问题出在表单上,但是自动代码生成器使用命名空间而不是建立表单。错误表单以为问题出在表单的第19行,但实际上是在program.cs文件的开头。我希望这种情况不会经常发生。
我知道这是一个常见问题,但我找不到答案。我正在尝试 Microsoft Emotion API(我在这里使用通用键来提出问题),它一直给我错误,“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:");
...
}
我发现了问题,实际上不在提供的代码中。 IDE 认为问题出在表单上,但是自动代码生成器使用命名空间而不是建立表单。错误表单以为问题出在表单的第19行,但实际上是在program.cs文件的开头。我希望这种情况不会经常发生。