如何将我的应用程序启动时调用此异步方法在我Xamarin形式? [英] How can I call this async method in my Xamarin Forms when my app starts?

查看:361
本文介绍了如何将我的应用程序启动时调用此异步方法在我Xamarin形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序第一次开始,我需要加载一些previously保存的数据。如果它存在 - >然后转到这个TabbedPage页面。否则,一个登录页面。

When my app is first starting, I need to load up some previously saved data. If it exists -> then goto the TabbedPage page. Otherwise, a Login page.

我不知道我怎么能叫我的异步方法在应用程序的构造函数,甚至在其他的方法?

I'm not sure how I can call my async method in the app's ctor or even in another method?

我怎样才能做到这一点?

How can I do this?

下面是我的code ..

Here's my code..

namespace Foo
{
    public class App : Application
    {
        public App()
        {
            Page page;

            LoadStorageDataAsync(); // TODO: HALP!

            if (Account != null)
            {
                // Lets show the dashboard.
                page = new DashboardPage();
            }
            else
            {
                // We need to login to figure out who we are.
                page = CreateAuthenticationPage();
            }

            MainPage = page;
        }

  ... snip ...
}

那么,为什么 LoadStorageDataAsync 异步?因为它是使用库PCLStorage ,这是所有异步。

So why is LoadStorageDataAsync async? Because it's using the library PCLStorage and that is all async.

谁能帮助吗?

推荐答案

至于文档说的,你有 Application.OnStart 事件,则可以覆盖:

As far as the docs say, you have the Application.OnStart event which you can override:

应用程序开发人员重写此方法来执行操作时,
  应用程序启动。

Application developers override this method to perform actions when the application starts.

您可以执行你的异步方法,有它其实是可以期待的:

You can execute your async method there where it can actually be awaited:

public override async void OnStart()
{
    await LoadStorageDataAsync();
}

这篇关于如何将我的应用程序启动时调用此异步方法在我Xamarin形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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