2012-11-22 58 views
1

我正在製作一個非常簡單的軟件,它帶有一個TCP偵聽器,它直到現在才從TCP客戶端接收到一個ASCII格式的消息,不得不做一些我仍然不知道的UI,但現在,我只是試圖在Samsung Galaxy Tab上顯示一條AlertDialog消息。我看不到我的佈局(Monodroid + MS Visual Studio 2010)

問題是,我相信由於某種原因,setContentView不起作用。我有一個帶有AbsoluteLayout的.axml(佈局)文件,我在代碼上調用了AbsoluteLayout,更改了它的顏色,並試圖在屏幕上顯示AbsoluteLayout(其顏色已更改),但問題是我只是看到普通的黑屏。

我開始調試代碼,我可以看到MS VS 2010輸出中的所有Console.Writeline命令,甚至是從客戶端發送的消息。但是我看不到佈局和AlertDialog。

任何人都可以幫助我嗎?提前致謝。

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using Android.Graphics.Drawables; 
using System.Drawing; 

namespace Gafisa.Automacao.VideoWall.Listener 
{ 
    [Activity(Label = "Listener", MainLauncher = true, Icon = "@drawable/icon")] 
    public class Activity1 : Activity 
    { 
     protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.Main); 
     AbsoluteLayout abs = FindViewById<AbsoluteLayout>(Resource.Id.abslayout); 
     abs.SetBackgroundColor(new Android.Graphics.Color(125,125,125,125)); 
     //ImageButton btn = new ImageButton(this); 
     //var lp = new AbsoluteLayout.LayoutParams(50, 50, 200, 200); 
     //btn.LayoutParameters = lp; 
     //BitmapDrawable dd = new BitmapDrawable("/mnt/sdcard/1.png"); 
     //btn.SetBackgroundDrawable(dd); 
     //abs.AddView(btn); 

     System.Net.Sockets.TcpListener listener = null; 
     byte[] rcvBuffer = new byte[40]; 
     int bytesRcvd; 

     try 
     { 
      listener = new System.Net.Sockets.TcpListener(IPAddress.Any, 13000); 
      listener.Start(); 
      Console.WriteLine("Listener iniciado"); 
     } 
     catch (SocketException se) 
     { 
      Console.WriteLine("Erro ao iniciar o listener: " + se.Message); 
     } 

     for (;;) 
     { 
      TcpClient client = null; 
      NetworkStream netStream = null; 
      try 
      { 
       client = listener.AcceptTcpClient(); 
       netStream = client.GetStream(); 
       int totalBytesEchoed = 0; 
       while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0) 
       { 
        netStream.Write(rcvBuffer, 0, bytesRcvd); 
        totalBytesEchoed += bytesRcvd; 
       } 
       string recebido = System.Text.Encoding.ASCII.GetString(rcvBuffer); 
       Console.WriteLine(recebido); 

       AlertDialog.Builder alert = new AlertDialog.Builder(this); 
       alert.SetMessage(recebido); 
       alert.SetTitle("Mensagem Recebida"); 
       alert.Show(); 
       Console.WriteLine("echoed {0} bytes.", totalBytesEchoed); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
       Console.WriteLine("Erro no LOOP"); 
      } 
      finally 
      { 
       netStream.Close(); 
       client.Close(); 
      } 
     } 
    } 
} 

}

回答

0

通過運行在此OnCreate功能的無限循環,您可以防止在完成渲染UI框架。這就是爲什麼你看到的只是黑屏。

您應該異步運行非UI代碼(在單獨的線程中)。

0

將其更改爲

Task.Factory.StartNew(() => 
{ 
    for (;;) 
     { 
      TcpClient client = null; 
      NetworkStream netStream = null; 
      try 
      { 
       client = listener.AcceptTcpClient(); 
       netStream = client.GetStream(); 
       int totalBytesEchoed = 0; 
       while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0) 
       { 
        netStream.Write(rcvBuffer, 0, bytesRcvd); 
        totalBytesEchoed += bytesRcvd; 
       } 
       string recebido = System.Text.Encoding.ASCII.GetString(rcvBuffer); 
       Console.WriteLine(recebido); 
       RunOnUiThread(() => 
       { 
        AlertDialog.Builder alert = new AlertDialog.Builder(this); 
        alert.SetMessage(recebido); 
        alert.SetTitle("Mensagem Recebida"); 
        alert.Show(); 
       } 
       Console.WriteLine("echoed {0} bytes.", totalBytesEchoed); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
       Console.WriteLine("Erro no LOOP"); 
      } 
      finally 
      { 
       netStream.Close(); 
       client.Close(); 
      } 
     } 
    } 
使用Mono的Android(Xamarin.Android)

你也應該使用Log.Info(string tag, string message)其中標籤是調用類的名稱。請勿使用Console.WriteLine(string)。您也可以使用Log.Warn(string, string)Log.Error(string, string)

相關問題