简体   繁体   English

如何在Xamarin android和ios中安装线程?

[英]How to sleep thread in Xamarin android and ios?

I am building an application for both iOS and Android using Xamarin cross platform. 我正在使用Xamarin跨平台为iOS和Android构建应用程序。 I have one project called "Core", and then one for iOS and one for Android. 我有一个名为“Core”的项目,然后一个用于iOS,一个用于Android。

In the shared "Core" project I want to show a loading animation that I made, while fetching the current location from the device. 在共享的“核心”项目中,我想显示一个加载动画,同时从设备中获取当前位置。 I can get my locationWatcher, start it, and call LocationWatcher.CurrentPosition directly, but since it has just started watching for location updates, it hasn't yet had time to get any location. 我可以获取我的locationWatcher,启动它,并直接调用LocationWatcher.CurrentPosition,但由于它刚刚开始观察位置更新,它还没有时间获取任何位置。

My solution to this is to wait 2 seconds after starting it and then get the location. 我的解决方案是在启动后等待2秒然后获取位置。 However I don't want to wait asynchroneously, I want the UI to be locked while waiting. 但是我不想等待异步,我想在等待时锁定UI。 This is because the user should not be able to do anything until current position os known. 这是因为用户在当前位置已知之前不应该做任何事情。

I currently achieve this by having a loop: 我目前通过循环来实现这一目标:

DateTime startTime = DateTime.Now();
while ( (DateTime.Now() - startTime).TotalMilliseconds() < 2000) {
    //Locking the UI here, but it's what I want so it's ok.
}

This seems like an ugly 'hack' for something that in Java would simply be "Thread.sleep(2000)" 对于Java中的“Thread.sleep(2000)”来说,这似乎是一个丑陋的“黑客”

Is there a better way of doing this? 有没有更好的方法呢?

The multiplatform way with Xamarin Forms would be to 使用Xamarin Forms的多平台方式将是

await Task.Delay(ms);

Edit: 编辑:

After reading that you really want to block the mainthread heres the multiplatform way to block a Thread: 在阅读之后你真的想要阻止mainthread继承阻止线程的多平台方式:

Task.Delay(ms).Wait()

Blocking the UI thread is bad practice, since among other things it could cause the device to mark the app as "not responding". 阻止UI线程是不好的做法,因为除其他外,它可能导致设备将应用程序标记为“无响应”。 You are also not guaranteed that the position will have been determined in two seconds, so in the best case your suggestion is only a partial fix. 您也无法保证该位置将在两秒内确定,因此在最好的情况下,您的建议只是部分修复。

What you'll really want to do is to block UI input by disabling the possibility to give input (there should be methods to disable your UI elements) and then see if there's a callback from the geolocation service when it has determined your location. 您真正想要做的是通过禁用提供输入的可能性来阻止UI输入(应该有禁用UI元素的方法),然后在确定您的位置时查看地理定位服务是否有回调。 In the callback, re-enable the UI elements. 在回调中,重新启用UI元素。 If such a callback does not exist, perform the location lookup synchronously on the UI thread with RunOnUIThread/InvokeOnMainThread, so that the thread is blocked only until it returns. 如果不存在这样的回调,则使用RunOnUIThread / InvokeOnMainThread在UI线程上同步执行位置查找,以便仅在线程返回之前阻止该线程。

If you want to go ahead with your solution anyway, 如果您想继续使用您的解决方案,

RunOnUiThread(() => Thread.Sleep(2000));

should do the trick, but I really recommend against doing that. 应该做的伎俩,但我真的建议不要这样做。 On iOS, you'll have to do 在iOS上,你必须这样做

InvokeOnMainThread(() => Thread.Sleep(2000));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM