C# 控制反轉


主要目的:需要在業務邏輯層調用UI的彈框提示信息
代碼是在記事本中寫的,沒有運行調試過,不保證能直接使用,看下思路就好

//接口
public
interface IShowData { void ShowMsg(string msg); } //UI層 public class ShowUI:IShowData { public void ShowMsg(string msg) {
     //使用Invock調用方法,這樣能讓方法在UI線程中執行
this.Invoke(new deleShowMsg(ShowMessageBox),msg); }
   //定義委托,調用方式時使用
public delegate void deleShowMsg(object msg);
   //彈出窗體的方法
public void ShowMessageBox(object obj) { MessageBox.Show(obj.ToString()); }
   //窗體加載事件
public void ShowUI_Load(object sender, EventArgs e) { ShowBLL bll = new ShowBLL();
     //把自己注冊到BLL里面 bll.SetShowMsg(
this); } } //BLL業務邏輯層 public class ShowBLL { IShowData Ishow=null; public void SetShowMsg(IShowData ishow) { Ishow = ishow; }
   //在任意方法中調用
public void fun() { Ishow.ShowMsg("彈出顯示信息"); } }
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM