简体   繁体   中英

“FormsApp is a namespace but used like type”

I know this is a frequently asked question, but I couldn't find an answer. I am trying out a Microsoft Emotion API (I've used the generic key here for the purpose of asking the question), and it keeps giving me the error, " WindowsFormsApp2 is a namespace but is used as a type" even when I change the namespace. I changed the namespace to a more appropriate title, but I still received the build error that WindowsFormsApp2 was inappropriately used as a type, despite the fact it was nowhere in the code. I don't know where else I'm using it that it is creating this issue.

Here is my code:

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);
            }



        }

    }
}

I'd dislike the semicolon after InitializeComponent() in your form.

        InitializeComponent();   <--- !!!
        {
            textBox1.Text = ("Enter the path to a JPEG image file:");
            ...
        }

I found the problem, which is actually not in the code provided. The IDE thought the problem was in the form, but the automatic code generator used the namespace instead of establishing the form. The error form thought the problem was in line 19 of the form, but it was actually in the beginning of the program.cs file. I hope this doesn't happen often.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM