我正在製作一個程序,連接到多個第三方系統。連接不同的格式,所以我創建了多個類來處理它們。我現在有三個4班。C#多個類事件
MainForm是第一類。這是帶有用戶界面的基本Windows窗體類。
SDKCommunication是第二類。
VMS(此類處理由二路政黨制度給出的事件並激活SDK通訊方式)
活動
活動類
public class Events
{
public event EventHandler LoginStateChanged;
private bool loginstate;
public bool LogInState
{
get { return this.loginstate; }
set
{
this.loginstate = value;
if (this.LoginStateChanged != null)
this.LoginStateChanged(this, new EventArgs());
}
}
}
SDKCommunicatie類的一部分
Events events = new Events();
public void onLogon(string username, string directory, string system)
{
events.LogInState = false;
}
MainForm類
SDKCommunicatie sdkcommunicatie = new SDKCommunicatie();
Events events = new Events();
public MainForm()
{
InitializeComponent();
events.LoginStateChanged += new EventHandler(events_LoginStateChanged);
}
void events_LoginStateChanged(object sender, EventArgs e)
{
log.Info("EventFired loginstateChanged");
}
當LogInState變化在SDKCommunicatie類。需要在MainForm類中觸發一個事件。但可悲的是,這是行不通的。 但是,當我更改mainform中的loginstate(使用buttonclick)時(參見下面的代碼),事件被觸發。但那不是我想要的意圖。
private void button1_Click(object sender, EventArgs e)
{
events.LogInState = true;
}
如果我的問題不夠清楚,請告訴我。
VMS類添加爲答覆@Astef
class VMS {
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(MainForm));
GxUIProxyVB m_UIProxy = new GxUIProxyVB();
public string username2;
public string directory2;
public string Status;
public void initOmni()
{
m_UIProxy.CreateInstance();
m_UIProxy.OnLogon += new _IGxUIProxyVBEvents_OnLogonEventHandler(m_UIProxy_OnLogon);
m_UIProxy.OnLogoff += new _IGxUIProxyVBEvents_OnLogoffEventHandler(m_UIProxy_OnLogoff);
m_UIProxy.OnError += new _IGxUIProxyVBEvents_OnErrorEventHandler(m_UIProxy_OnError);
m_UIProxy.OnAlarmStatusEx2 += new _IGxUIProxyVBEvents_OnAlarmStatusEx2EventHandler(m_UIProxy_OnAlarmStatusEx2);
}
public void login(string username, string password, string directory)
{
username2 = username;
directory2 = directory;
initOmni();
m_UIProxy.LogOn(directory, username, password,false);
}
public void logOff()
{
m_UIProxy.LogOff();
}
void m_UIProxy_OnLogon()
{
SDKCommunicatie sdkcommunicatie = new SDKCommunicatie();
sdkcommunicatie.onLogon(username2, directory2, "Genetec Omnicast");
}
我有固定的這個與刪除下列:
SDKCommunicatie sdkcommunicatie = new SDKCommunicatie();
並增加在VMS的基礎下列內容:
SDKCommunicatie sdkcommunicatie;
但現在我得到了一個新的錯誤的MainForm當我試圖調用類的SDKCommunicatie
connectedStatus = sdkcommunicatie.connectedStatus();
我得到了以下錯誤:
NullReferenceException was unhandled
1.遠程協作調試你的代碼不是這個地方的目的。可能是你最好使用http://codereview.stackexchange.com/ 2.如果你想編程,你需要學習如何自己調查異常。通常有很多這是正常的。 3.你的項目設計很糟糕,瞭解固體原理(未來可能) – astef 2013-05-07 08:48:49
對不起,如果這是錯誤的做法。我很害怕用C#進行編程,而對於stackoverflow來說是新的。在學習C#時,我使用了很多教程和本站,但從未在本網站上提出任何問題。 – Gulpener 2013-05-07 08:58:10