vb.net 如何在多项目、单一解决方案环境(VS2012)中调用Forms?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30082895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:06:04  来源:igfitidea点击:

How to call Forms in multi projects, single solution environment (VS2012)?

.netvb.netwinforms

提问by pdsd

This is partially related to this thread Combining two projects and get a single .sln file.

这部分与此线程Combining two projects and get a single .sln file 相关

What are the correct syntax to call the forms on these projects. For example, if Solution1 contains Project1 and Project2, ...and... Project1 has Form1.vb & Project2 has Form1.vb. So what is the syntax to call Form1.vb in Project2 from Form1.vb in Project1 (assuming there is a button to click and open a form on click event).

在这些项目上调用表单的正确语法是什么。例如,如果 Solution1 包含 Project1 和 Project2,...并且...Project1 具有 Form1.vb,而 Project2 具有 Form1.vb。那么从 Project1 中的 Form1.vb 调用 Project2 中的 Form1.vb 的语法是什么(假设有一个按钮可以在单击事件时单击并打开表单)。

Just a note however, I've added Project1 & Project2 to Solution1 as well as added a reference to My Project.Resources.Designer.vb.dll.

但是,请注意,我已将 Project1 和 Project2 添加到 Solution1,并添加了对 My Project.Resources.Designer.vb.dll 的引用。

But when I tried to call Form1.vb in Project2 from Project1, I got syntax error - Project2.Form1 is not defined.

但是当我尝试从 Project1 调用 Project2 中的 Form1.vb 时,出现语法错误 - Project2.Form1 未定义。

Can someone point me in the right direction?

有人可以指出我正确的方向吗?

Any help is greatly appreciated. Thanks in advance.


任何帮助是极大的赞赏。提前致谢。


Project1 is bold hence the startup project.

Project1 是粗体,因此是启动项目。

enter image description here

在此处输入图片说明





Public Class Form1 of Project 1: -

项目 1 的公开课 Form1:-

enter image description here

在此处输入图片说明





Public Class Form1 of Project 2: -

项目2的公开课Form1:-

enter image description here

在此处输入图片说明





Error message: -

错误信息: -

enter image description here

在此处输入图片说明





Don't have the option to select "Import Namespace": -

没有选择“导入命名空间”的选项:-

enter image description here

在此处输入图片说明





This is how the form Project1 looks like: -

这是表单 Project1 的样子:-

enter image description here

在此处输入图片说明





My Reference Manager => Solution option is empty

我的参考管理器 => 解决方案选项为空

enter image description here

在此处输入图片说明





Let say, if I want to browse to the reference file in Solution => Projects option above, which file type should I choose ??
a. Visual Basic Project file
b. USER File
c. VSPSCC File

比方说,如果我想在上面的解决方案 => 项目选项中浏览参考文件,我应该选择哪种文件类型??
一种。Visual Basic 项目文件
B. 用户文件
C. VSPSCC 文件

enter image description here

在此处输入图片说明





How to call Form1 (in olAddIn_With_Form1) from Project1 (Startup project) ?

Answer:
Add the .dll via Reference Manager window then browse to ...\bin\Debug\olAddIn_With_Form1.dll

Dim myolAddIn_With_Form1Form1 As New olAddIn_With_Form1.Form1 myolAddIn_With_Form1Form1.ShowDialog()

如何从 Project1(启动项目)调用 Form1(在 olAddIn_With_Form1 中)?

答案:
通过引用管理器窗口添加 .dll 然后浏览到 ...\bin\Debug\olAddIn_With_Form1.dll

Dim myolAddIn_With_Form1Form1 As New olAddIn_With_Form1.Form1 myolAddIn_With_Form1Form1.ShowDialog()

enter image description here

在此处输入图片说明





For kicks, I try to add the whole project via "Add as link" method and I got this error message

对于踢球,我尝试通过“添加为链接”方法添加整个项目,但收到此错误消息

enter image description here

在此处输入图片说明

回答by Icepickle

So, to answer with some screenshots:

所以,用一些截图来回答:

First create your two projects. The project that is the startup project (in your example and in mine, that would be Project1) needs to know about the other solution. To do this, we need to add the reference to the project, right click on Project1 and click on 'Add reference...'

首先创建您的两个项目。作为启动项目的项目(在您和我的示例中,即 Project1)需要了解其他解决方案。为此,我们需要添加对项目的引用,右键单击 Project1 并单击“添加引用...”

add reference to startup project

添加对启动项目的引用

Then, use the solution option in the sidebar, to click the checkbox on Project2

然后,使用侧栏中的解决方案选项,单击 Project2 上的复选框

select second project

选择第二个项目

And then you can add the project in your code using the Project2.Form1 identifier, as such

然后您可以使用 Project2.Form1 标识符在代码中添加项目,因此

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim frmOtherProject As New Project2.Form1
        frmOtherProject.Show()
    End Sub
End Class

or, in case your form in the second project doesn't have a biased name (form1 currently exists 2 times, so lets rename it as form2), you can import the second project and use it's classes directly as such

或者,如果您在第二个项目中的表单没有偏向名称(form1 当前存在 2 次,因此让我们将其重命名为 form2),您可以导入第二个项目并直接使用它的类

Imports Project2

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim frmOtherProject As New Form2
        frmOtherProject.Show()
    End Sub
End Class

I used Visual Studio 2012 as a basis, but the principle should be the same ;)

我使用Visual Studio 2012作为基础,但原理应该是一样的;)

ADDITION

添加

I might point out that this will not be your typical style of referencing the projects, mostly you will separate your application by levels of concern, by adding, for example:

我可能会指出,这不是您引用项目的典型风格,大多数情况下,您将按关注级别将应用程序分开,例如添加:

  • An entitylayer, that contains your models, and is referenced by all other layer (solutions)
  • A datalayer that can load/save your models and serve them, this is referenced by the "business logic layer" and references the entitylayer itself
  • A business logic layer, that references the datalayer and the entitylayer and that is referenced inside the presentation layer, it doesn't know anything about which database you are using, it just serves as an intermediary between your presentation layer (which is what the user sees) and the datalayer, and only handles the entities defined in your entity layer
  • And long last, the presentation layer, that references the entity layer and the business logic layer, it to doesn't need to know which database is serving the data, but only presents the data in a useful way to it's users
  • 一个实体层,包含您的模型,并被所有其他层(解决方案)引用
  • 一个可以加载/保存您的模型并为其提供服务的数据层,这是由“业务逻辑层”引用并引用实体层本身
  • 一个业务逻辑层,它引用数据层和实体层,并在表示层内部引用,它不知道您正在使用哪个数据库,它只是作为表示层之间的中介(这是用户sees) 和数据层,并且只处理实体层中定义的实体
  • 最后是表示层,它引用了实体层和业务逻辑层,它不需要知道哪个数据库正在为数据提供服务,而只需要以一种有用的方式将数据呈现给它的用户

There are of course plenty of ways to arrange your application in a meaningful structure, but I find that this one is a good example on how you can structure your application in a meaningful way

当然有很多方法可以用有意义的结构来安排你的应用程序,但我发现这个是一个很好的例子,说明你如何以一种有意义的方式来组织你的应用程序

UPDATE

更新

As an update still, getting your solutions to share code, shouldn't be this hard, if you write your code so that it can be reused. By sharing the logic and the harder code inside a class library that can be shared over both solutions, you only have to rewrite the Presentation layer (how you display the data). And you can do it more specifically for the environment you want to work with.

作为更新,如果您编写代码以便可以重用,那么让您的解决方案共享代码应该不难。通过在可以在两种解决方案上共享的类库中共享逻辑和更难的代码,您只需重写表示层(如何显示数据)。并且您可以更具体地针对您想要使用的环境执行此操作。

In the end, your Outlook Addin Solution and Your windows forms project could share the code that requests the resources, or loads the data, or does some other complex calculations, and the only code you have to "reproduce" is how you show it on the screen. So according to the environment you can present the data in a better way, specific to that environment, but share the logic and the models you use in both (or more) environments.

最后,您的 Outlook 插件解决方案和您的 windows 窗体项目可以共享请求资源、加载数据或执行其他一些复杂计算的代码,而您唯一需要“重现”的代码就是您如何在其上显示它屏幕。因此,根据环境,您可以以更好的方式呈现特定于该环境的数据,但共享您在两种(或更多)环境中使用的逻辑和模型。

This way, you development time is cut down, and your code becomes less error prone, because you don't have the same code several times, as an example, see the following screenshot:

这样,你的开发时间就减少了,你的代码也不容易出错,因为你没有多次使用相同的代码,例如,请看下面的截图:

enter image description here

在此处输入图片说明

As you can see, there is a shared library, that is referenced by the outlook addin and by the windows form application. Neither the Forms application nor the outlook application know about each other, and they also shouldn't, as they have essentially nothing to do with the other.

如您所见,有一个共享库,由 Outlook 插件和 Windows 窗体应用程序引用。Forms 应用程序和 Outlook 应用程序都不知道彼此,它们也不应该知道,因为它们彼此之间基本上没有任何关系。

So, although, my latest update doesn't answer your question, I still think it's the better way to arrange your code. If you would later want to make a website reusing the code you made, you only need to make an extra presentation layer, and reuse the code from the SharedLibrary once again.

因此,尽管我的最新更新没有回答您的问题,但我仍然认为这是安排代码的更好方式。如果您以后想重用您制作的代码制作一个网站,您只需要制作一个额外的表示层,并再次重用 SharedLibrary 中的代码。

回答by Shar1er80

I have a solution that has a C# project and a VB.Net project.

我有一个包含 C# 项目和 VB.Net 项目的解决方案。

When I right click on My Projectin my VB.Net project I'm given an Open context menu

当我右键单击My Project我的 VB.Net 项目时,我会看到一个打开的上下文菜单

enter image description here

在此处输入图片说明

When I click open, I get the following screen

当我点击打开时,我得到以下屏幕

enter image description here

在此处输入图片说明

I click the Add button and get this screen

我单击“添加”按钮并显示此屏幕

enter image description here

在此处输入图片说明

The project that is listed here is my C# project that I can add and in my VB.Net project and I can call upon any public classes in that project.

此处列出的项目是我可以在 VB.Net 项目中添加的 C# 项目,我可以调用该项目中的任何公共类。

回答by SSS

  1. Solution Explorer>Project>Right-click>Add>Existing Item
  2. Browse & select the file
  3. -- here's the trick -- click the arrow next to "Add" and select "Add as link"
  1. 解决方案资源管理器>项目>右键单击>添加>现有项目
  2. 浏览并选择文件
  3. -- 这就是诀窍 -- 单击“添加”旁边的箭头并选择“添加为链接”

This allows multiple projects to share code files without requiring a shared dll, references, or anything. Each app can stay as a standalone EXE.

这允许多个项目共享代码文件,而无需共享 dll、引用或任何东西。每个应用程序都可以作为一个独立的 EXE。

回答by bautista

How to accomplish this task without references:

如何在没有参考的情况下完成此任务:

I'd like to add another approach since I've come across this problem too. The only difference is that I couldn't add a reference to my second project since it already had a existing reference. Trying to do so would result in the following error:

我想添加另一种方法,因为我也遇到过这个问题。唯一的区别是我无法添加对我的第二个项目的引用,因为它已经有一个现有的引用。尝试这样做会导致以下错误:

A reference to 'XXXX' could not be added. Adding this project as a reference would cause a circular dependency.

无法添加对“XXXX”的引用。将此项目添加为引用会导致循环依赖。

So here is a working approach (for me):

所以这是一个工作方法(对我来说):

In my calling class:

在我的通话课上:

'Path of the .exe which contains the form that should be opened
Dim allAssemblies As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(Application.ExecutablePath)

'loads content of the assembly file
Dim runTimeTypes As Type() = allAssemblies.GetTypes

'iterate the content
For Each runTimeType As Type In runTimeTypes

 'if matching record is found
 If runTimeType.Name = "F_myForm" Then

  'open it with the desired parameters; of course a matching constructor has to exist in "F_myForm"
  Dim parameters As Object() = {"x", "y", "z"}

  Dim form As Form = CType(Activator.CreateInstance(runTimeType, parameters), Form)

  form.Show()

 End If

Next