简体   繁体   English

连接到Web服务器时的Xamarin进度指示器

[英]Xamarin progress indicator while connecting to web server

I'm facing a problem with Async operation when App starts. 应用启动时,我遇到了异步操作问题。

During OnCreate , I start a background operation that call a web service to test if the service is available. OnCreate期间,我启动一个后台操作,该操作调用Web服务以测试该服务是否可用。 In the activity, I have a label with Connecting... text. 在活动中,我有一个带有正在Connecting...文本的标签。 When the async operation completes, the label's text is changed to OK or No service . 异步操作完成后,标签的文本将更改为OKNo service

Now the question is: can I show to my user that an async operation is in progress? 现在的问题是:我可以向用户显示异步操作正在进行中吗?

I cannot figure out how I can use another async operation can show some animation while the server responds and complete the second async operation when the first completes. 我无法弄清楚如何使用另一个异步操作可以在服务器响应时显示一些动画,并在第一个异步操作完成时完成第二个异步操作。

You would ideally create a custom View which renders the animation (or use the SDK built-in indeterminate ProgressBar ) and place it in your layout, with its visibility set to ViewStates.Gone . 理想情况下,您将创建一个自定义View来呈现动画(或使用SDK内置的不确定的ProgressBar )并将其放置在布局中,其visibility设置为ViewStates.Gone

Before the async operation is launched, the view's visibility is set to ViewStates.Visible , and after the async operation completes, the visibility is again set to Gone . 启动异步操作之前,视图的visibility设置为ViewStates.Visible ,而异步操作完成后, visibility再次设置为Gone You may even remove the view from the layout if you want after the async operation completes. 如果需要,甚至可以在异步操作完成后从布局中删除视图。

You'd have something like (pseudo-C#) 你会有类似(伪C#)的东西

async void OnCreate()
{
    // Some code (get references to views, etc.)

    progressView.Visibility = ViewStates.Visible;

    var asyncResult = await SomeAsyncOperation();

    progressView.Visibility = ViewStates.Gone;

    // Some more code
}

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

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