引用同一程序集中的不同项目,不同的名称空间(Referencing a different project in the same assembly, different namespaces)

我有两个项目:Menu和Module,它们都在同一个名称空间foobar中。

我目前正在从菜单项目中引用模块项目,以在菜单中的选项卡控件上打开某些控件。 但是,我需要从我的一个控件启动一个新控件,该控件位于Module项目中。

当我尝试引用菜单项目时,当我尝试使用using来引用它时,它不会显示在我的intellisense中。 我在逻辑上做错了吗?

这是一个例子:

项目菜单

Public Void LaunchWPFControl(string pHeader,string pPath) { //Code goes here to launch a WPF control in the browser }

项目模块

//What I would love to do but doesn't work Using Menu; ... ... ... private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e) { Menu.LaunchWPFControl("Accounts","AccountsControl"); }

I have two projects : Menu and Module and they are both in the same namespace foobar.

I am currently referencing the module project from the Menu project to open up certain controls on a tab control in my menu. However I need to launch a new control from one of my controls which is located in the Module project.

When I try referencing the menu project, it does not show up in my intellisense when I try to reference it with a using. Am I doing something wrong logically here?

Here is an example of what it is :

Project Menu

Public Void LaunchWPFControl(string pHeader,string pPath) { //Code goes here to launch a WPF control in the browser }

Project Module

//What I would love to do but doesn't work Using Menu; ... ... ... private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e) { Menu.LaunchWPFControl("Accounts","AccountsControl"); }

最满意答案

如果你在谈论单独的项目,那么你在这里尝试做的是循环引用,这是不允许的。 如果项目菜单引用项目模块,则项目模块无法引用项目菜单。

如果您需要Project Module中的类来触发Menu项目中的某些内容,则需要寻找另一种方法。 实现此目的的一种可能技术是在Module项目的类中创建一个事件,Menu项目可以订阅该事件并执行所需的操作。

例如,在项目模块中:

private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e) { OnMyEvent(); } private void OnMyEvent() { EventHandler localEvent = MyEvent; if(localEvent != null) { localEvent(this, EventArgs.Empty); } }

然后在项目菜单中,您可以订阅此活动并执行您的操作:

... ... ... moduleClass.MyEvent += SomeHandler; ... ... ... private void SomeHandler(Object sender, EventArgs e) { Menu.LaunchWPFControl("Accounts","AccountsControl"); }

正如Ray Burns所提到的(参见注释)另一种方法是在某个共享位置(有引用的项目或其他一些共享项目)定义Menu类的接口,而不是将该接口的实现传递给Module项目。

哪种方式更好通常取决于您尝试使用每个项目实现的抽象。

If you are talking about seperate projects then what you are trying to do here is a circular reference, this is not allowed. If Project Menu references Project Module, then Project Module cannot reference Project Menu.

If you need a class in Project Module to trigger something in the Menu project you need to look for an alternative way of doing it. One possible technique for achieving this is to create an event in the class in the Module project that the Menu project can subscribe to and perform the required action.

For example in Project Module:

private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e) { OnMyEvent(); } private void OnMyEvent() { EventHandler localEvent = MyEvent; if(localEvent != null) { localEvent(this, EventArgs.Empty); } }

Then in Project Menu you can subscribe to this event and perform your action:

... ... ... moduleClass.MyEvent += SomeHandler; ... ... ... private void SomeHandler(Object sender, EventArgs e) { Menu.LaunchWPFControl("Accounts","AccountsControl"); }

As Ray Burns mentions (see comments) another way would be to define an interface of the Menu class in some shared location (either there referenced project, or some other shared project) and than you can pass implementations of that interface to the Module project.

Which way is better often depends on the abstraction you are trying to achieve with each project.

更多推荐