c# - C# android 中的回调

标签 c# java android

我已经在 Handler 上实现了回调(在 java 中)。我需要在 c#(Xamarin) 上实现相同的功能。但到目前为止,我无法找到任何解决方案来解决如何在 C# 中执行此操作。我是 C# 新手,因此知识很少。

这是java代码:-

private Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg)
    {
      if (msg.what == MSG_SURFACE_CREATED)
      {
        contentWidth = 0;
        contentHeight = 0;
        requestLayout();
        return true;
      } 
      else
      {
        Log.w("Unknown msg.what: " + msg.what);
      }
      return false;
    }
  });

有什么想法可以在 C# 中实现相同的功能吗?

最佳答案

事件是可行的方法(使用委托(delegate))。这是一些示例代码:

class CallingClass
{
    private SurfaceCreatingClass m_surfacecreatingclass;

    public CallingClass()
    {
        m_surfacecreatingclass = new SurfaceCreatingClass();
        m_surfacecreatingclass.SurfaceCreatedHandler += OnFinished;
        m_surfacecreatingclass.CreateSurface();
    }

    void OnFinished(int iMessageWhat)
    {
        if (iMessageWhat == SurfaceCreatingClass.MSG_SURFACE_CREATED)
        {
            contentWidth = 0;
            contentHeight = 0;
            RequestLayout();
        }
        else
        {
            Log.w("Unknown msg.what: " + iMessageWhat);
        }
    }
}

class SurfaceCreatingClass
{
    public delegate void SurfaceCreatedDelegate(int iWhat);
    public event SurfaceCreatedDelegate SurfaceCreatedHandler;
    public const int MSG_SURFACE_CREATED = 1;

    public void CreateSurface()
    {
        /////////////////////////////
        // Surface creation code here
        // ...
        /////////////////////////////

        if (SurfaceCreatedHandler != null)
            SurfaceCreatedHandler(MSG_SURFACE_CREATED);
    }
}

关于c# - C# android 中的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20492329/

相关文章:

Java Hashmap Iterator 条件问题

java - 用 Volley 上传多张图片?

java - Android 中的序列化

android - 无法在 Retrofit 2.0 中解析具有两种不同数据类型的 json

android - 谷歌地图 Android V2 和 Direction API

c# - 从 C# 中的文件字段流式传输

c# - 如何转换实现接口(interface)的具体类的列表

c# - 如何在 EPPlus C# .XLSX 下载中添加交替行格式

c# - 如何从其他类的独立线程上运行的方法中获取变量?

java - JSP中图片上传问题