gpt4 book ai didi

c# - Xamarin Android 中不等待调用异步方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:49:48 25 4
gpt4 key购买 nike

我有一个 fragment ,用于返回从 REST API 收到的文章列表。 REST API 是 async我使用 await等待它完成。完成后,我将数据(即 List<Article> )传递给名为 listofarticles 的变量并使用它来创建 ArrayAdapter .

我在 OnCreate 中执行上述代码这个 fragment 的方法应该在 OnCreateView 之前运行我所知道的。

我的代码在 Activity 中有效,但在 fragment 中无效。

问题:
不等待对可等待 REST 方法的调用。

当逐行调试时,它是这样的:

  • OnCreate叫做。处理至 我在这里调用 awaitable 方法(应该填充 listofarticles )并简单地跳到 OnCreateView .因此listofarticles一片空白。
  • OnCreateView被调用和处理。此时程序只是回到它被调用的 Activity 。
  • 我无法再调试它,因为 Xamarin Studio 认为我已经完成了,但是在模拟器中它只是开始调用 REST 方法,因为这一切都在 listofarticles 中。没用,因为我现在无法将它传递给适配器。

  • 为什么会发生这种情况以及如何解决?
    public class LatestNewsFragment : Android.Support.V4.App.Fragment, ListView.IOnItemClickListener
    {
    private Context globalContext = null;
    private List<Article> listofarticles;
    private ArrayAdapter<Article> listAdapter;

    public LatestNewsFragment()
    {
    this.RetainInstance = true;
    }

    public async override void OnCreate (Bundle savedInstanceState)
    {
    base.OnCreate (savedInstanceState);
    globalContext = this.Context;

    //Create a progress dialog for loading
    ProgressDialog pr = new ProgressDialog(globalContext);
    pr.SetMessage("Loading data");
    pr.SetCancelable(false);

    var rest = new RestAccess();
    pr.Show();
    //Download the data from the REST API
    listofarticles = await rest.ListArticlesAsync ("SomeSource");
    pr.Hide();
    Console.WriteLine (listofarticles.Capacity);

    listAdapter = new ArrayAdapter<Article>(globalContext, Android.Resource.Layout.SimpleListItem1, listofarticles);
    }

    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    var view = inflater.Inflate(Resource.Layout.Fragment_LatestNews, null);
    ListView listView = view.FindViewById<ListView>(Resource.Id.list_latestnews);
    listView.Adapter = listAdapter;
    listView.OnItemClickListener = this;
    return view;
    }
    }

    最佳答案

    如本答案 https://stackoverflow.com/a/32656807/1230302 中所述在 fragment 中做任何与 OnCreate() 相关的 View 都不是一个好主意。

    此外,如果您将 OnCreate() 设为异步方法,其他 fragment 生命周期事件(请参阅 http://developer.android.com/guide/components/fragments.html )将在 await 行之后立即调用。现在的问题是你不能依赖等待之后代码​​运行的顺序,如果你的 REST API 很慢 OnCreateView() 可能已经完成并且你会没事的,但是如果 API 很快 OnCreateView() 可能仍然运行你的应用程序会崩溃。

    为什么不使用 OnActivityCreated() 加载数据?您可以确定所有内容都已在其中初始化,例如:

    private ListView listView;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    var view = inflater.Inflate(Resource.Layout.Fragment_LatestNews, null);

    listView = view.FindViewById<ListView>(Resource.Id.list_latestnews);
    listView.OnItemClickListener = this;

    return view;
    }

    public override async void OnActivityCreated(Bundle savedInstanceState)
    {
    base.OnActivityCreated(savedInstanceState);

    //Download the data from the REST API
    var rest = new RestAccess();
    var listofarticles = await rest.ListArticlesAsync("SomeSource");

    listView.Adapter = new ArrayAdapter<Article>(Context, Android.Resource.Layout.SimpleListItem1, listofarticles);
    }

    关于c# - Xamarin Android 中不等待调用异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758948/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com