简体   繁体   English

将用户控件从一个项目复制到同一解决方案中的另一个项目

[英]Copy User Control from one project to another within same solution

Occasionally, a C# project will have a user control in it that belongs to another C# project and I will have to move it over.有时,一个 C# 项目中会有一个属于另一个 C# 项目的用户控件,我必须将它移过去。 Attempting to simply copy + paste that user control over results in namespace errors.试图简单地复制 + 粘贴用户控制会导致命名空间错误。 Let's say project 1 has a namespace of namespace General.Category1.Controls and project 2 has a namespace of namespace General.Category2.Controls .假设项目 1 的命名空间为namespace General.Category1.Controls ,项目 2 的命名空间为namespace General.Category2.Controls

The steps I initially took were:我最初采取的步骤是:

1) Copy + paste control (which includes .cs , designer.cs and .resx files) from project 1 to project 2 1)从项目 1 到项目 2 复制 + 粘贴控件(包括.csdesigner.cs.resx文件)

2) Change namespace to project 2's namespace General.Category2.Controls 2) 将命名空间更改为项目 2 的namespace General.Category2.Controls

At this point upon building, I received additional errors from the forms on which this control is used before the copy + paste that states 'The type of namespace name 'UserControl1' does not exist in namespace 'General.Category1.Controls' (are you missing an assembly reference?) .在构建的这一点上,我在复制+粘贴之前从使用此控件的表单中收到了其他错误,指出'The type of namespace name 'UserControl1' does not exist in namespace 'General.Category1.Controls' (are you missing an assembly reference?) It turns out that the designer.cs form files did not update to the correct namespace after the copy + paste.事实证明,在复制 + 粘贴后, designer.cs表单文件没有更新到正确的命名空间。

So then I add to add an additional step:那么我添加一个额外的步骤:

3) Change declarations to match new namespace of General.Category2.controls aka private General.Category2.Controls UserControl userControl1; 3)更改声明以匹配General.Category2.controls aka private General.Category2.Controls UserControl userControl1;新命名空间private General.Category2.Controls UserControl userControl1;

I'm confused why the designer.cs files wouldn't have updated that since it's not usually advised to edit designer.cs files.我很困惑为什么designer.cs文件不会更新它,因为通常不建议编辑designer.cs文件。 But then again, maybe it's ok to modify them as long as you don't touch the InitializeComponent() method as it states in the actual file itself.但话又说回来,也许可以修改它们,只要您不触及实际文件中声明的InitializeComponent()方法。

In conclusion, why do all of the designer.cs not update automatically?总之,为什么所有的designer.cs没有自动更新? Is the actual answer to know where this control is used and update the declarations myself as I have done?是否知道在哪里使用此控件并像我所做的那样自己更新声明的实际答案?

[EDIT]: The first 2 steps are for the control itself (it's .cs and .designer.cs files). [编辑]:前两个步骤是针对控件本身的(它是.cs.designer.cs文件)。 The 3rd step is for associated with any forms in which the control was placed on before copying it over because it is still using the old namespace, so all of the form .designer.cs files must be changed as well.第 3 步是与任何在复制之前放置控件的表单相关联,因为它仍在使用旧命名空间,因此所有表单.designer.cs文件也必须更改。

Been there !到过那里 ! I do not use copy-paste for this.我不为此使用复制粘贴。 In VS-2017 a UserControl1 can be copied to another project, as follows:在 VS-2017 中,可以将 UserControl1 复制到另一个项目,如下所示:

  • In Windows: copy UserControl1.cs, .designer.cs and .resx into your new project directory在 Windows 中:将 UserControl1.cs、.designer.cs 和 .resx 复制到您的新项目目录中
  • In VS: right click your project in Solution Explorer, set it as Startup project在 VS 中:在解决方案资源管理器中右键单击您的项目,将其设置为启动项目

  • Right click new project in Solution Explorer, and choose Add Existing Item在解决方案资源管理器中右键单击新项目,然后选择添加现有项目

  • Now, select only UserControl1.cs file in the file box现在,在文件框中选择UserControl1.cs文件

  • Wait, Solution Explorer will register the other files, change the symbol等待,解决方案资源管理器将注册其他文件,更改符号
  • In VS, open UserControl1.designer.cs .. change namespace to new project namespace在 VS 中,打开UserControl1.designer.cs .. 将命名空间更改为新的项目命名空间
  • In VS, open UserControl1.cs, change namespace to new project namespace在 VS 中,打开UserControl1.cs,将命名空间更改为新的项目命名空间

After this step, rebuild your new project.在这一步之后,重建你的新项目。 Go to the form design and open Toolbox.转到表单设计并打开工具箱。 Toolbox will show your UserControl. Toolbox 将显示您的 UserControl。

Note if you move your control into a new Framework or Core class library, take into account, that WinForms is not referenced yet !请注意,如果您将控件移动到新的 Framework 或 Core 类库中,请考虑到 WinForms 尚未被引用! Make sure you add a reference to System.Windows.Forms before adding existing items using my tips above.在使用我上面的提示添加现有项目之前,请确保添加对 System.Windows.Forms 的引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将服务引用从一个项目复制到同一解决方案中的另一个项目 - Copy service reference from one project to another project in same solution WinRT-如何在同一解决方案中使用来自另一个项目的自定义控件? - WinRT - How to use custom control from another project within the same solution? 将所有数据从一个用户控件复制到相同类型的另一用户控件 - Copy all the data from one user control to another user control of Same Type 在同一解决方案中从另一个项目中的一个项目打开xaml页面 - Open a xaml page from one project in another project in the same solution 在同一解决方案中将一个项目的照片文件用于另一个项目 - Use photo file from one project to another project in the same solution 如何在同一个解决方案中将一个项目的功能包含到另一个项目中? - How to include functionality from one project into another in the same solution? 在同一解决方案中使用从一个项目到另一个项目的依赖关系 - Use dependency from one project to another in the same solution 如何在同一解决方案中执行另一个项目? - How to execute another project within the same solution? 如何将用户控件.ascx添加到同一解决方案中另一个项目中的Web窗体 - How to add a user control .ascx to a web form which is in another project in the same solution 是否可以从同一解决方案中将项目A中的一页重定向到项目B中的页面? - Is it possible to redirect from one page in Project A to a page in Project B from within the same solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM