【问题标题】:I can't see my Layout (Monodroid + MS Visual Studio 2010)我看不到我的布局(Monodroid + MS Visual Studio 2010)
【发布时间】:2012-11-22 01:45:04
【问题描述】:

我正在使用 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();
            }
        }
    }
}

}

【问题讨论】:

    标签: android user-interface xamarin.android


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      改成

       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();
                  }
              }
          }
      

      对于 Android 的 Mono (Xamarin.Android),您应该使用 Log.Info(string tag, string message),其中标记是调用类的名称。不要使用Console.WriteLine(string)。你也可以使用Log.Warn(string, string)Log.Error(string, string)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多