有一个Windows Forms项目,我从默认repos复制到桌面的一个文件夹,打开复制的项目时,应该显示WindowsForms布局的页面(构造函数),报错:
文件“E:\Users\mikkel\source\repos\WindowsFormsApp1\WindowsFormsApp1\Form1.cs”不支持代码解析或自动代码生成,因为它不包含在代码感知项目中。
我刚刚注意到错误中指出了项目的旧路径,尽管 Form1.cs 也存在于复制的文件夹中。从默认存储库打开同一项目时,不会发生此错误。我还注意到 InitializeComponent(); 方法在程序代码中没有突出显示,尽管它应该是黄色的。
Form1.cs 类代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
if (textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
{
CellPhone phone = new CellPhone();
phone.Manufacturer = textBox3.Text;
phone.Model = textBox4.Text;
phone.IsOld = checkBox1.Checked ? true : false;
try
{
phone.Number = Convert.ToInt32(textBox5.Text);
textBox2.Text += $"Производитель: {phone.Manufacturer} " + Environment.NewLine + $"Модель: {phone.Model}" + Environment.NewLine + $"Б / У: {phone.IsOld}" + Environment.NewLine + $"Количество: {phone.Number}" + Environment.NewLine;
}
catch { MessageBox.Show("Кол-во указывается только цифрами."); }
}
else MessageBox.Show("Не все поля заполнены!");
}
}
public class CellPhone
{
public string Manufacturer;
public string Model;
public int Number;
public bool IsOld;
public CellPhone(string manufacturer, string model, int number, bool isOld)
{
Manufacturer = manufacturer;
Model = model;
Number = number;
IsOld = isOld;
}
public CellPhone()
{
Manufacturer = "unknown manufacturer";
Model = "unknown model";
Number = 0;
IsOld = false;
}
}
}
在我看来,您需要在某个地方指定 Form1 类的新路径,但我不知道这是在哪里完成的。
我在正确的文件夹中创建一个新项目并将设计文件和form1类文件复制到源中找到了问题的解决方案