UI层引用了BLL层,当UI层new新线程调用BLL中A函数执行大量业务逻辑时,这时A函数要向UI层textBox不断打印信息,我在UI层中写了一个打印函数W,可是BLL无法引用UI层,系统提示“未能引用UI,将导致循环依赖项”。 |
|
![]() |
不应该是 bll调用ui打印信息的,
应该是ui调用bll,bll返回打印信息给ui来打印, 或者ui在调用bll的时候,传一委托过去,让bll层调用的 |
![]() 20分 |
使用委托就可以了:
BLL中: pre class=”brush: csharp”>namespace BLL { public class Test { public delegate void RefreshDelegate(ref string msg); public void Call(RefreshDelegate d) { string msg = string.Empty; for (int i = 1; i <= 10; i++) { msg = string.Format(“{0}\r\n”, i); d(ref msg); } } } } UI层: pre class=”brush: csharp”>namespace CSWin { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void btn_Test_Click(object sender, EventArgs e) { BLL.Test test = new BLL.Test(); BLL.Test.RefreshDelegate d = new BLL.Test.RefreshDelegate(Refresh); test.Call(d); } private void Refresh(ref string msg) { this.textBox1.Text += msg; } } } |
![]() |
不能相互引用的,只需要ui引用bll即可
|
![]() |
dll的引用只能按顺序层次依赖,不能循环依赖的
|
![]() |
使用接口,利用依赖倒置原则,可解决循环依赖的问题
|
![]() |
觉得委托是正解。
|