在同一程序集中引用不同的项目,不同的命名空间

发布于 2024-09-05 10:52:03 字数 658 浏览 2 评论 0原文

我有两个项目:菜单和模块,它们都位于同一名称空间 foobar 中。

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

当我尝试引用菜单项目时,当我尝试使用 using 引用它时,它不会显示在我的智能感知中。我在这里逻辑上做错了什么吗?

这是一个示例:

项目菜单

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");
}

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

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

发布评论

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

评论(3

蓝海 2024-09-12 10:52:03

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

如果您需要项目模块中的类来触发菜单项目中的某些内容,您需要寻找替代方法来执行此操作。实现此目的的一种可能的技术是在 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 类的接口(或者引用项目) ,或其他一些共享项目),然后您可以将该接口的实现传递给模块项目。

哪种方法更好通常取决于您试图在每个项目中实现的抽象。

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.

心奴独伤 2024-09-12 10:52:03

如果它们都在命名空间 foobar 中,那么您需要

using foobar;

而不是 using Menu

了解所涉及的术语很重要 - 您正在谈论“同一程序集中的不同项目” - 这是无意义的。 Visual Studio 为每个项目创建一个程序集。然后,您可以讨论问题文本中具有相同命名空间的项目。您需要了解其中的差异。

要计算出类型的命名空间,请打开包含该类型的类并查找命名空间声明:

namespace MyProject
{
    ...
}

要计算出类型的程序集,请在项目属性中查找声明该类型的项目 -或者,如果您使用相同的解决方案,只需将想要使用该类型的项目中的引用添加到声明该类型的项目中即可。

请注意,您使用 using 指令指定命名空间;您需要在解决方案资源管理器中添加对程序集的引用。他们做的不是同一件事。 using 指令只是说,“在受此 using 指令影响的代码中,我希望能够使用此命名空间中的类型,而无需完全限定名称。”

接下来,您将得到如下代码:

Menu.LaunchWPFControl("Accounts","AccountsControl");

我认为 Menu 要么是项目名称,要么是命名空间 - 但现在您尝试将其用作类型名称。是哪一个?

如果这不能解决您的问题,请发布完整代码以及所涉及的项目和命名空间的更连贯的描述。退后一步,弄清楚所涉及的类型、命名空间和程序集,然后将其全部清晰地列出来。

If they're both in namespace foobar then you need

using foobar;

instead of using Menu.

It's important that you understand the terminology involved - you're talking about "a different project in the same assembly" - that's nonsensical. Visual Studio creates one assembly per project. You then talk about the projects having the same namespace in the text of your question. You need to understand the difference.

To work out the namespace of a type, open the class containing the type and look for namespace declarations:

namespace MyProject
{
    ...
}

To work out the assembly of a type, look in the project properties for the project in which it's declared - or if you're using the same solution, just add a reference from the project which wants to use the type to the project which declares the type.

Note that you specify a namespace with a using directive; you need to add a reference to an assembly in solution explorer. They're not doing the same thing. A using directive just says, "Within the code affected by this using directive, I want to be able to use types within this namespace without fully qualifying the names."

Next you've got code like this:

Menu.LaunchWPFControl("Accounts","AccountsControl");

I thought Menu was either a project name or a namespace - but now you're trying to use it as a type name. Which is it?

If this doesn't sort you out, please post full code and a more coherent description of the projects and namespaces involved. Take a step back, work out the types, namespaces and assemblies involved, and then lay it all out clearly.

倾听心声的旋律 2024-09-12 10:52:03

所以项目菜单引用项目模块。那么您希望项目模块引用项目菜单,以便模块可以直接调用菜单功能吗?

这是不可能的,这是循环引用。如果您尝试将菜单引用添加到模块项目,Visual Studio 不会允许您这样做。

您需要将菜单和模块都想要使用的内容从菜单中提取到第三个项目中,并让它们都引用它。或者将菜单和模块合并到一个项目中。

So Project Menu references Project Module. Then you want Project Module to reference Project Menu so a module can directly call Menu functionality?

This isn't possible, this is a circular reference. If you try to add the Menu reference to the Module project, Visual Studio won't let you.

You need to pull the stuff out of Menu that both Menu and Module want to use into a third project, and have them both reference it. Or combine Menu and Module into one project.

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