在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();
}
}
答案 0 :(得分:1)
如果您调试代码,您将看到当发生时会发生什么!所以试试这个,我相信你会惊讶于解决你的问题是多么容易。
无论如何,您设置邮件的方式定时错误。请尝试使用此代码。它删除消息状态(函数编程)并在收到消息后设置消息。
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;
}
}