为什么在Xamarin应用程序中使用Device.BeginInvokeOnMainThread()? [英] Why use Device.BeginInvokeOnMainThread() in a Xamarin application?

查看:236
本文介绍了为什么在Xamarin应用程序中使用Device.BeginInvokeOnMainThread()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

    public void Init() {
        if (AS.pti == PTI.UserInput)
        {
            AS.runCardTimer = false;
        }
        else
        {
            AS.runCardTimer = true;
            Device.BeginInvokeOnMainThread(() => showCards().ContinueWith((arg) => { }));
        }
    }

从构造函数中调用Init方法.有人可以向我解释为什么开发人员可能添加Device.BeginInvokeOnMainThread()而不是仅调用showCards方法吗?

The Init method is called from the constructor. Can someone please explain to me why the developer might have added the Device.BeginInvokeOnMainThread() instead of just calling the method showCards?

ContinueWith((arg))还能做什么?为什么要包含它?

Also what does the ContinueWith((arg)) do and why would that be included?

推荐答案

可能在后台线程上创建此Init()方法所在的类.我假设showCards()正在更新某种UI. UI只能在UI/Main线程上更新. Device.BeginInvokeOnMainThread()确保lambda内部的代码在主线程上执行.

The class where this Init() method is might be created on a background thread. I'm assuming showCards() are updating some kind of UI. UI can only be updated on the UI/Main thread. Device.BeginInvokeOnMainThread() ensures that the code inside the lambda is executed on the main thread.

ContinueWith()是可以在Task上找到的方法.如果showCards()返回任务,则ContinueWith()确保退出lambda之前任务已完成.

ContinueWith() is a method which can be found on Task. If showCards() returns a task, ContinueWith() makes sure the task will complete before exiting the lambda.

这篇关于为什么在Xamarin应用程序中使用Device.BeginInvokeOnMainThread()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆