Im新的C#和框架林玩和我試圖找出一些代碼如何工作(沒有什麼錯的代碼)。它是一個客戶端/服務器應用程序,它將一些文本從客戶端發送到服務器,然後在文本框中接收並顯示相同的字符串。 以下代碼來自客戶端及其表單。這裏只包含從服務器接收字符串的東西。我收錄了框架中的一些評論。幫助與代表解釋代碼
public class TestModuleMobile : PreCom.Core.ModuleBase, PreCom.Core.IForm
{
public delegate void ReceiveDelegate(string data);
public event ReceiveDelegate DataReceived;
public void Receive(byte[] data)
{
string text = Encoding.UTF8.GetString(data, 0, data.Length);
if (DataReceived != null)
DataReceived.Invoke(text);
}
public override bool Initialize()
{
PreCom.Application.Instance.Communication.Register(99, Receive);
// Register(uint receiverID, RecieveDelegate receiver): Called by modules to register for communication.
//
// Parameters:
// receiverID:
// Module Id
// receiver:
// The module receive function that will be called by the framework when data
// arrives to specific module. (This method should return as soon as possible
// to avoid timeouts)
_isInitialized = true;
return true;
}
}
public partial class TestModuleMobileForm : PreCom.Controls.PreComForm
{
TestModuleMobile _module;
public TestModuleMobileForm(TestModuleMobile module)
{
_module = module;
_module.DataReceived += new TestModuleMobile.ReceiveDelegate(DataReceived);
InitializeComponent();
}
void DataReceived(string data)
{
if (InvokeRequired)
{
ThreadStart myMethod = delegate { DataReceived(data); };
this.BeginInvoke(myMethod);
return;
}
listBox1.Items.Insert(0, data);
this.preComInput21.Text = "";
}
}
問題:
1.公共覆蓋布爾初始化()
的函數調用來註冊需要ReceiveDelegate對象作爲第二個參數。那麼當它只是一個函數時,我怎麼能發送一個函數給它(Receive)呢?這個怎麼用?
2. public void Receive(byte [] data)
if-case中會發生什麼?如何調用工作?
3. void DataReceived(string data)
if-case(行一行)會發生什麼?
可能(甚至因爲你是新來的.net)你應該得到一份[C#深入](http://www.amazon.com/dp/1935182471)的副本,並看看第2.5章快速跟蹤的代表。 – Oliver 2011-05-26 10:03:38