1

我正在使用 TCP 侦听器制作一个非常简单的软件,它(直到现在)只接收来自用 ASCII 编码的 TCP 客户端的消息,我将不得不对我仍然不知道的 UI 做一些事情,但现在,我只是想在三星 Galaxy Tab 上显示带有此消息的 AlertDialog。

问题是,我相信由于某种原因 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();
            }
        }
    }
}

}

4

2 回答 2

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 for Android (Xamarin.Android),您应该使用Log.Info(string tag, string message)where tag 是调用类的名称。不要使用Console.WriteLine(string). 您还可以使用Log.Warn(string, string)Log.Error(string, string)

于 2012-11-22T08:50:19.360 回答
0

通过在此函数中运行无限循环OnCreate,您可以防止 UI 框架完成渲染。这就是为什么你看到的只是黑屏。

您应该异步运行非 UI 代码(在单独的线程中)。

于 2012-11-22T06:18:09.563 回答