为什么不接受消息?
Why does not accept messages?
在 WinForms 下工作,所以没有。
将来,我想以某种方式连续显示所有帖子。但是直到这个阶段还没有到,因为现在连一条消息都不被接受。
public class MainActivity : Activity
{
public string mess;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
StartListening ();
Button bt = FindViewById<Button>(Resource.Id.button1);
bt.Click += delegate { start();};
// Get our button from the layout resource, and attach an event to it
}
public void start()
{
TextView text = FindViewById<TextView> (Resource.Id.textView1);
StartListening();
text.Text = mess;
}
private readonly UdpClient udp = new UdpClient(45000);
public void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
public void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 45000);
byte[] bytes = udp.EndReceive(ar, ref ip);
mess = Encoding.ASCII.GetString(bytes);
StartListening();
}
}
如果你调试你的代码,你就会看到什么时候会发生什么!所以试试这个,我相信您会惊讶于解决您的问题是多么容易。
总之,您设置消息定时的方式不正确。请尝试使用此代码。它删除消息状态(函数式编程)并在收到消息后设置消息。
public class MainActivity : Activity
{
private readonly UdpClient udp = new UdpClient(45000);
protected override void OnCreate (Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
StartListening();
Button bt = FindViewById<Button>(Resource.Id.button1);
bt.Click += delegate { StartListening(); };
}
public void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
public void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 45000);
byte[] bytes = udp.EndReceive(ar, ref ip);
DisplayMessage(Encoding.ASCII.GetString(bytes));
StartListening();
}
public void DisplayMessage(string message)
{
FindViewById<TextView>(Resource.Id.textView1).Text = message;
}
}
在 WinForms 下工作,所以没有。
将来,我想以某种方式连续显示所有帖子。但是直到这个阶段还没有到,因为现在连一条消息都不被接受。
public class MainActivity : Activity
{
public string mess;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
StartListening ();
Button bt = FindViewById<Button>(Resource.Id.button1);
bt.Click += delegate { start();};
// Get our button from the layout resource, and attach an event to it
}
public void start()
{
TextView text = FindViewById<TextView> (Resource.Id.textView1);
StartListening();
text.Text = mess;
}
private readonly UdpClient udp = new UdpClient(45000);
public void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
public void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 45000);
byte[] bytes = udp.EndReceive(ar, ref ip);
mess = Encoding.ASCII.GetString(bytes);
StartListening();
}
}
如果你调试你的代码,你就会看到什么时候会发生什么!所以试试这个,我相信您会惊讶于解决您的问题是多么容易。
总之,您设置消息定时的方式不正确。请尝试使用此代码。它删除消息状态(函数式编程)并在收到消息后设置消息。
public class MainActivity : Activity
{
private readonly UdpClient udp = new UdpClient(45000);
protected override void OnCreate (Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
StartListening();
Button bt = FindViewById<Button>(Resource.Id.button1);
bt.Click += delegate { StartListening(); };
}
public void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
public void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 45000);
byte[] bytes = udp.EndReceive(ar, ref ip);
DisplayMessage(Encoding.ASCII.GetString(bytes));
StartListening();
}
public void DisplayMessage(string message)
{
FindViewById<TextView>(Resource.Id.textView1).Text = message;
}
}