使用 EnvDTE 创建表单后不会保存表单

发布于 2024-10-20 11:03:39 字数 1868 浏览 5 评论 0原文

我创建了一个 Visual Studio 加载项,它将一个表单添加到打开的解决方案中的现有项目中。

这就是我创建表单的方式:

string templatePath = sol.GetProjectItemTemplate("Form.zip", "csproj");
//sol is an EnvDTE80.Solution2

proj.ProjectItems.AddFromTemplate(templatePath, formName);
//proj is an EnvDTE.Project

之后我可以成功获取对添加表单的ProjectItem的引用,然后我可以获取对System.Windows.Forms.Form的引用,通过这个引用我可以添加一个按钮像这样的表单:

Button btn = new Button();

btn.Text = "my funky button";
btn.Name = "newButton";
btn.Size = new Size(150, 23);
btn.Location = new Point(30, 30);

frmNewForm.Controls.Add(btn);
//frmNewForm is a System.Windows.Forms.Form

然后按钮成功添加到表单中:

在此处输入图像描述

但是,当我尝试保存此表格,它只是不会保存。我单击 Visual Studio 的[保存]按钮,它只是没有变成灰色。即使我单击[全部保存],该表单也不会被保存。然后我关闭 Visual Studio,重新打开它,然后打开我已使用外接程序添加新表单的项目,但新按钮根本不存在。只是一个空表格。

我什至尝试通过以下代码以编程方式保存项目和解决方案:

itemForm.Save(itemForm.Name);
//itemFrom is an EnvDTE.ProjectItem

proj.Save(proj.FullName);
//proj is an EnvDTE.Project

我认为这是因为我用来测试加载项的 Visual Studio 实例是调试实例,在之后立即打开我运行我的加载项。但我尝试使用已安装的加载项(运行后自动保留),问题仍然存在。


更新
我刚刚注意到两件事:

1) 该按钮仅出现在表单的设计上,而不出现在其他地方。它甚至不允许我选择它来查看它的属性。

它的名称不会出现在 Intellisense 中、对象列表中,甚至表单的设计文档中。

作为测试,我手动添加了一个按钮,我可以选择该按钮并与其交互:

在此处输入图像描述

我可以从中得到的是我没有正确添加按钮。

那么关于按钮的新问题将是:如何将新按钮添加到通过 EnvDTE 创建的表单中,以便我可以在设计时与之交互?

2) 在尝试查看我的时髦按钮和手动添加按钮的差异时,我尝试了以前通过编程创建的表单从未尝试过的操作:实例化并运行它。

我是这样做的:

MyFunkyForm frm = new MyFunkyForm ();
frm.Show();

它在屏幕上闪烁(显然没有我的两个按钮),然后执行结束。即使我尝试在 Form_load 上执行某些操作,它也会被执行,然后表单闪烁,然后执行结束(表单关闭,调试结束),就像我调用了 Close() 方法一样。

那么额外的问题是:我在添加表单时是否跳过了一个步骤,或者我根本没有正确创建它?

I have created a Visual Studio Add-In that adds a form to an existing Project in the opened solution.

This is how I create the form:

string templatePath = sol.GetProjectItemTemplate("Form.zip", "csproj");
//sol is an EnvDTE80.Solution2

proj.ProjectItems.AddFromTemplate(templatePath, formName);
//proj is an EnvDTE.Project

After that I can successfully get the reference to the ProjectItem of the added form, then I can get a reference to the System.Windows.Forms.Form, and through this reference I can add a button to the form like this:

Button btn = new Button();

btn.Text = "my funky button";
btn.Name = "newButton";
btn.Size = new Size(150, 23);
btn.Location = new Point(30, 30);

frmNewForm.Controls.Add(btn);
//frmNewForm is a System.Windows.Forms.Form

And then the button is successfully added to the form:

enter image description here

However, when I try to save this form, it just won’t save. I click Visual Studio’s [save] button, it just doesn’t turn gray. Even if I click [Save All], the form won’t be saved. Then I close Visual Studio, reopen it, and open the project to which I have added the new form with my Add-In, and the new button simply isn’t there. Just an empty form.

I’ve even tried saving the project and the solution programmatically through the following code:

itemForm.Save(itemForm.Name);
//itemFrom is an EnvDTE.ProjectItem

proj.Save(proj.FullName);
//proj is an EnvDTE.Project

I’ve thought that this would be because the instance of the Visual Studio that I was using to test my Add-In is a debug one, opened right after I run my Add-In. But I’ve tried using the installed Add-In (which remained automatically after running it), and the problem persisted.


Update
I’ve just noticed two things:

1) the button only appears on the design of the form, and nowhere else. And it doesn’t even let me select it to see it’s attributes.

It’s name doesn’t appear in Intellisense, in the object list, or even on the design document of the form.

As a test, I’ve added a button manually, and this one I can select, and interact with it:

enter image description here

What I could get from that is that I am not adding the button properly.

Then the new question regarding the button would be: How can I add a new button to a form created through EnvDTE in a way that I can interact with it in design time ?

2) While trying to see the differences from my funky button and my Manually added button, I’ve attempted something I hadn’t before with a programmatically created form: instantiate and run it.

And here's how I've done it:

MyFunkyForm frm = new MyFunkyForm ();
frm.Show();

It flashes on the screen (apparently with none of my two buttons), and the execution ends. Even if I try to do something on it’s Form_load, it’s executed, then the form flashes, and the execution is ended (the form is closed and debugging is over), as if I had called the Close() method.

Then the additional question would be: Did I skip a step while adding the form, or am I not creating it properly at all ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-10-27 11:03:39

您正在向 ac# 项目添加一个表单,我认为您不应该像您那样单独实例化该表单,但如果您想查看它,您应该执行该应用程序。

我不知道该怎么做,从来没有尝试过自己,发现了这个,希望它有帮助:

http://www.mztools.com/articles/2006/mz2006016.aspx

you are adding a form to a c# project, I think you should not instantiate the form alone by itself like you did but if you want to see it you should execute the application.

I don't know how to do this, never tried myself, found this, hopefully it's helpful:

http://www.mztools.com/articles/2006/mz2006016.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文