我如何调用异步函数xamarin

时间:2016-11-17 02:21:14

标签: c# asynchronous xamarin xamarin.forms

我正在尝试调用异步函数,但是当我尝试执行它时,它下面有红线。我希望在页面打开时调用该函数,提前感谢。

public partial class Home : ContentPage
{


    public class GoogleProfile
    {
        public string Id { get; set;}

    }


    public Home()
    {
        InitializeComponent();
    }


    protected override async void OnAppearing()
    {
        await Check(/*What do i put in here*/);

    }

    public async Task Check(GoogleProfile googleprofile)
    {


        if (String.IsNullOrEmpty(googleprofile.Id))
        {

        }
        else {
            await Navigation.PushAsync(new LoginPage());
        }
    }




}

我怎么称呼这个?对不起我是C#和xamarin的新手

1 个答案:

答案 0 :(得分:1)

您应该在compile 'junit:junit:4.12' androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) androidTestCompile ('com.android.support.test:runner:0.5', { exclude group: 'com.android.support', module: 'support-annotations' }) androidTestCompile ('com.android.support.test:rules:0.5', { exclude group: 'com.android.support', module: 'support-annotations' }) compile('com.android.support.test.espresso:espresso-intents:2.2.1', { exclude group: 'com.android.support', module: 'support-annotations' }) 进行异步调用。正如您的名字所示,将在您的页面显示时调用ContentPage.OnAppearing()事件。这是用户的预期行为。另请注意,我更改了OnAppearing()方法以返回Check(),因为如果您能够编辑方法签名,请始终尝试将异步方法的返回类型从Task更改为{{ 1}}:

void

从技术上讲,如果您已经死定不使用Task,那么在推送public partial class LoginPage : ContentPage { public LoginPage() { InitializeComponent(); } protected override async void OnAppearing() { await Check(/* Add code here to get your GoogleProfile object */); } public async Task Check(GoogleProfile googleprofile) { var ID = googleprofile.Id; if (string.IsNullOrEmpty(ID)) { return; } else { await Navigation.PushAsync(new Home()); } } } 之前可以执行OnAppearing(),虽然没有看到更多代码,但这似乎会打败Check()的目的。