【问题标题】:Moving from monotouch to monodroid - async and threading porting从 monotouch 到 monodroid - 异步和线程移植
【发布时间】:2013-07-06 19:57:18
【问题描述】:

我有一个小型 iOS 应用程序(使用 monotouch 编写),我想将它引入 monodroid。该端口导致了几个问题,其中一些问题归结于两个平台执行 UI 和围绕它们创建类的方式。

在 iOS 应用中有这样的代码

private void BtnSomething_TouchUpInside (object sender, EventArgs e)
    {
        string f = this.foo;
                    LGPM gpm = new LGPM(Constants.a, Constants.b, f);
        this.auth = new Auth("cheese", gpm);
    (*) this.auth.TokenReceived += (o, e, a, aTS, r) => {
                    // more stuff here
        };

        this.PresentModalViewController(this.auth, true);
    }

auth 类看起来像这样

public partial class Auth
{
    public Auth(string m, data d)
    {
       this.d = d;
       this.m = m;
    }

 // create a UIWebView and do things

Upshot - auth 创建 webview,执行操作并将控制权返回到 (*) 行

对于 monodroid,情况有所不同,因为您无法真正创建这样的类。我想出的最好的就是这个

private void BtnSomething_TouchUpInside (object sender, EventArgs e)
    {
        string f = this.foo;
                    LGPM gpm = new LGPM(Constants.a, Constants.b, f);
        this.auth = new Auth("cheese", gpm, context);
    (*) this.auth.TokenReceived += (o, e, a, aTS, r) => {
                    // more stuff here
        };

        this.PresentModalViewController(this.auth, true);
    }

然后在 Auth 类中

public class Auth : Application
{
    public Auth(string m, data d, Context c)
    {
       this.d = d;
       this.m = m;
       Intent t = new Intent(this, typeof(webview));
       t.PutExtra("todo", 1);
       c.StartActivity(t);
    }
}

[Activity]

这是一个“正常”的 webview 活动。

这似乎可行,但是,一旦 web 视图完成,控制就不会返回到 (*) 行。

webview 本身正在执行来自网站的异步数据抓取(称为 AuthToken),一旦完成就会引发一个事件。我不确定这是否取决于两者之间类和活动的编写方式不同,但在 iOS 版本中,事件被触发,在 Android 版本中,它被错过了。

这让我想知道平台处理异步事件的方式是否不同。是否有关于这两个平台如何处理异步事件的区别的教程?

我知道很多问题,但线程和异步事件很重要。

谢谢

【问题讨论】:

    标签: xamarin.ios xamarin.android


    【解决方案1】:

    在一些处理事件之后使用 startActivityForResult 或其他 Android-ism 获取线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2012-12-13
      相关资源
      最近更新 更多