wpf 添加这个项目会导致循环依赖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22150543/
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
Adding this project will cause a circular dependency
提问by GibboK
I have a WPF application composed by two assemblies.
我有一个由两个程序集组成的 WPF 应用程序。
Assembly A is a wpf application.
程序集 A 是一个 wpf 应用程序。
Assembly B is a 3D app whit a public API mainly used by A.
Assembly B 是一个 3D 应用程序,带有一个主要由 A 使用的公共 API。
Assembly A has a reference to assembly B.
程序集 A 引用了程序集 B。
Now when an event fires on assembly B I need to call a method in assembly A in a static class app.
现在,当程序集 BI 触发事件时,需要在静态类应用程序中调用程序集 A 中的方法。
If I try to add a reference to assembly A in B I get an error:
如果我尝试在 BI 中添加对程序集 A 的引用,则会出现错误:
adding this project will cause a circular dependency
添加此项目将导致循环依赖
Do you have any ideas on how to solve it?
你对如何解决它有什么想法吗?
Specifically I would like to know:
具体我想知道:
- How to call
App.myMethod()in A fromassembly B? - In order to decouple the classes, could I fire a generic event on assembly B and catch it on assembly A?
- Any other ideas are welcome.
- 如何
App.myMethod()从 A调用assembly B? - 为了解耦这些类,我可以在程序集 B 上触发一个通用事件并在程序集 A 上捕获它吗?
- 欢迎任何其他想法。
回答by D Stanley
How do I call
App.myMethodin A from assembly B?
我如何
App.myMethod从程序集 B 中调用A?
You can't if A depends on B.
如果 A 依赖于 B,你就不能。
If the event is firedfrom some class in B you should subscribeto that event from some class in A:
如果事件是从 B 中的某个类触发的,您应该从 A 中的某个类订阅该事件:
code in A:
A中的代码:
var classB = new ClassB(); // presumably in your code in A somewhere
classB.MyEvent += new EventHandler(this.HandleEventFromB);
public void HandleEventFromB(object sneder, EventArgs args)
{
ClassA.StaticMethod();
}
That way B is not dependent on A.
这样 B 不依赖于 A。
You may need to "bubble up" the event within B to some publicly exposed event in order to subscribe to it from A.
您可能需要将 B 中的事件“冒泡”到一些公开的事件,以便从 A 订阅它。
回答by WraithNath
You can add it if you add a reference to the built DLL, instead of the project.
如果添加对构建的 DLL 的引用而不是项目,则可以添加它。
回答by BenM
Sounds like a design issue. As i'm not aware of the specific details in your implementation take the following as a guideline/idea.
听起来像是设计问题。由于我不知道您的实施中的具体细节,因此将以下内容作为指导/想法。
When the event fires on B I'm assuming it creates some data. If you could store this data in a database or similar and then have perhaps a timed event in application A that essentially listens for this data and then provides to update with it as necessary.
当事件在 B 上触发时,我假设它会创建一些数据。如果您可以将此数据存储在数据库或类似数据库中,然后在应用程序 A 中可能有一个定时事件,该事件基本上侦听此数据,然后根据需要提供更新。

