如何从OnCreate-Android C#Xamarin调用此方法

如何从OnCreate-Android C#Xamarin调用此方法,c#,xamarin.android,C#,Xamarin.android,我想知道如何从OnCreate方法调用公共静态异步任务CopyAssetAsync(Activity-Activity) public static async Task CopyAssetAsync(Activity activity) { { var notesPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Perso

我想知道如何从OnCreate方法调用
公共静态异步任务CopyAssetAsync(Activity-Activity)

  public static async Task CopyAssetAsync(Activity activity)
    {
        {
            var notesPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "NotesData.txt");

            if (!File.Exists(notesPath))
            {
                try
                {
更新1:

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            ActionBar.Hide();

            btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
            //Notes
            notesList = FindViewById<ListView>(Resource.Id.lvNotes);


            //Where I am trying to call the method...
            CopyAssetAsync();
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
btnAdd=findviewbyd(Resource.Id.btnAdd);
//注释
notesList=FindViewById(Resource.Id.lvNotes);
//我试图调用该方法的地方。。。
CopyAssetAsync();
更新2:

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            ActionBar.Hide();

            btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
            //Notes
            notesList = FindViewById<ListView>(Resource.Id.lvNotes);


            //Where I am trying to call the method...
            CopyAssetAsync();

调用时必须使用wait关键字,并使
OnCreate()
方法
异步

protected override async void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);
    ActionBar.Hide();

    btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
    //Notes
    notesList = FindViewById<ListView>(Resource.Id.lvNotes);


    //Where I am trying to call the method...
     await CopyAssetAsync(this);
}
protectedoverride async void OnCreate(Bundle)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
btnAdd=findviewbyd(Resource.Id.btnAdd);
//注释
notesList=FindViewById(Resource.Id.lvNotes);
//我试图调用该方法的地方。。。
等待CopySassetAsync(本);
}

调用时必须使用wait关键字,并使
OnCreate()
方法
async

protected override async void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);
    ActionBar.Hide();

    btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
    //Notes
    notesList = FindViewById<ListView>(Resource.Id.lvNotes);


    //Where I am trying to call the method...
     await CopyAssetAsync(this);
}
protectedoverride async void OnCreate(Bundle)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
btnAdd=findviewbyd(Resource.Id.btnAdd);
//注释
notesList=FindViewById(Resource.Id.lvNotes);
//我试图调用该方法的地方。。。
等待CopySassetAsync(本);
}

显示onCreate方法请参见onCreate方法更新1您的方法有1个参数,为什么不传递它?显示onCreate方法请参见onCreate方法更新1您的方法有1个参数,为什么不传递它?您需要传递活动类型引用,正如您声明为参数一样。如果您处于活动传递“this”作为当前活动。如果这对您有效,请接受并投票决定答案。我一直认为您不应该编写“async void”方法。相反,您应该返回“async Task”。但我在几篇文章中看到,人们正在重写OnCreate方法并将其设置为“async void”。Xamarin在异步/等待方法方面是否与其他受支持的平台有所不同?谢谢!:)您需要传递已声明为参数的活动类型引用。如果您在活动中,请传递“this”作为当前活动。如果这对您有效,请接受并投票决定答案。我一直认为您不应该编写“async void”方法。相反,您应该返回“async Task”。但我在几篇文章中看到,人们正在重写OnCreate方法并将其设置为“async void”。Xamarin在异步/等待方法方面是否与其他受支持的平台有所不同?谢谢!)