【问题标题】:Writing async monotouch code编写异步单点触控代码
【发布时间】:2012-12-10 00:24:13
【问题描述】:

所以我有一个必须执行三个任务的方法。 第一个任务必须在其他两个之前完成,可以并行执行。 这就是我现在所拥有的(为简单起见进行了修改)

void facebookButtonClicked (object sender, EventArgs e)
        {
            View.Add (loadingOverlay);
            AppDelegate.MobileService = new MobileServiceClient ("myserverUrl", "myApiKey");
            var users= AppDelegate.MobileService.GetTable<User> ();
            var identities= AppDelegate.MobileService
                .GetTable ("Identities");
            AppDelegate.MobileService
                .LoginAsync (this, MobileServiceAuthenticationProvider.Facebook)
                    .ContinueWith (t => {
                if (t.Status == TaskStatus.RanToCompletion) {
                            AppDelegate.mobileServiceUser = t.Result;
                            var task1 = users.InsertAsync(new User{BirthDate = DateTime.Now});
                            var task2 = identities.ReadAsync("");
                            Task.Factory.ContinueWhenAll(new Task[]{task1,task2},_=>{
                                if(task1.Status==TaskStatus.RanToCompletion && task2.Status== TaskStatus.RanToCompletion)
                                {
                                    var token = task2.Result
                                        .GetArray()[0]
                                        .GetObject ()
                                        .GetNamedObject ("identities")
                                        .GetNamedObject ("facebook")
                                        .GetNamedString ("accessToken");    

                                SaveAuthorization (token);
                                NavigationController.PushViewController (new TabBarController (), false);
                                }
                            });             
                        }});
        }

问题是(除了我是异步代码的新手)当它尝试执行“PushViewController”时,我得到:

UIKit.UIKitThreadAccessException: UIKit 一致性错误:你是 调用只能从 UI 线程调用的 UIKit 方法

如何重构/修改此代码以修复它? 请帮忙。

【问题讨论】:

    标签: .net asynchronous xamarin.ios task-parallel-library azure-mobile-services


    【解决方案1】:

    我调用 InvokeOnMainThread 并在我的异步方法中使用匿名函数。

    InvokeOnMainThread(() => {  
        NavigationController.PushViewController (new TabBarController(), false);
    });
    

    如果由于某种原因你在一个静态类中(就像我一样),你可以这样做

    new NSObject().InvokeOnMainThread(() => {   
        NavigationController.PushViewController (new TabBarController(), false);
    });
    

    【讨论】:

    • 如果任何其他困惑的新 Xamarin 开发人员遇到这个问题,这个答案与上面的答案基本相同,只是使用 lambda 表达式而不是委托方法,同时还包括静态类案例处理。
    【解决方案2】:

    看看 InvokeOnMainThread 或 BeginInvokeOnMainThread

    http://docs.xamarin.com/ios/Guides/Advanced_Topics/Threading#Developing_Responsive_Applications

    NavigationController.BeginInvokeOnMainThread (delegate { 
        NavigationController.PushViewController (new TabBarController (), false);
    }); 
    

    【讨论】:

    • 我没有检查这个,我面前没有编译器。让我知道它是否有错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 2020-04-15
    相关资源
    最近更新 更多