调用构造函数异步方法? [英] Call asynchronous method in constructor?

查看:124
本文介绍了调用构造函数异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:我想调用构造函数的异步方法。这可能吗?

详情:我有一个方法叫 getwritings()来解析JSON数据。一切工作正常,如果我只是叫 getwritings()异步方法,并把的await 来留下它。然而,当我在我的网页创建一个 LongListView ,并尝试填充它,我发现 getWritings()是令人惊讶的返回 LongListView 是空的。

要解决这个问题,我试图改变 getWritings的返回类型()任务<名单<书写>> ,然后通过 getWritings()。结果。然而,这样做最终的阻塞UI线程。

 公共部分类第2页:的PhoneApplicationPage
{
    清单<书写>著作;    公共第二页()
    {
        的InitializeComponent();
        getWritings();
    }    私人异步无效getWritings()
    {
        串jsonData =等待JsonDataManager.GetJsonAsync(1);
        JObject OBJ = JObject.Parse(jsonData);
        JArray阵列=(JArray)OBJ [上岗];        的for(int i = 0; I< array.Count;我++)
        {
            写作写作=新的写作();
            writing.content = JsonDataManager.JsonParse(数组,我,内容);
            writing.date = JsonDataManager.JsonParse(数组,我,日期);
            writing.image = JsonDataManager.JsonParse(数组,我,URL);
            writing.summary = JsonDataManager.JsonParse(数组,我,节选);
            writing.title = JsonDataManager.JsonParse(数组,我,称号);            writings.Add(书写);
        }        myLongList.ItemsSource =著作;
    }
}


解决方案

最好的解决办法是承认的下载和设计它的异步特性。

在换句话说,决定你的应用程序应该是什么样子,而数据的下载。在页面的构造函数设置的的看法,并开始下载。当下载完成的更新的页面中显示的数据。

我对异步构造一个博客帖子,你会发现有用的。此外,一些MSDN文章;一个在异步数据绑定(如果你使用MVVM),另一个上的异步最佳实践(也就是说,你应该避免异步无效)。

Summary: I would like to call an asynchronous method in a constructor. Is this possible?

Details: I have a method called getwritings() that parses JSON data. Everything works fine if I just call getwritings() in an async method and put await to left of it. However , when I create a LongListView in my page and try to populate it I'm finding that getWritings() is surprisingly returning null and the LongListView is empty.

To address this problem, I tried changing the return type of getWritings() to Task<List<Writing>> and then retrieving the result in the constructor via getWritings().Result. However, doing that ends up blocking the UI thread.

public partial class Page2 : PhoneApplicationPage
{
    List<Writing> writings;

    public Page2()
    {
        InitializeComponent();
        getWritings();
    }

    private async void getWritings()
    {
        string jsonData = await JsonDataManager.GetJsonAsync("1");
        JObject obj = JObject.Parse(jsonData);
        JArray array = (JArray)obj["posts"];

        for (int i = 0; i < array.Count; i++)
        {
            Writing writing = new Writing();
            writing.content = JsonDataManager.JsonParse(array, i, "content");
            writing.date = JsonDataManager.JsonParse(array, i, "date");
            writing.image = JsonDataManager.JsonParse(array, i, "url");
            writing.summary = JsonDataManager.JsonParse(array, i, "excerpt");
            writing.title = JsonDataManager.JsonParse(array, i, "title");

            writings.Add(writing);
        }

        myLongList.ItemsSource = writings;
    }
}

解决方案

The best solution is to acknowledge the asynchronous nature of the download and design for it.

In other words, decide what your application should look like while the data is downloading. Have the page constructor set up that view, and start the download. When the download completes update the page to display the data.

I have a blog post on asynchronous constructors that you may find useful. Also, some MSDN articles; one on asynchronous data-binding (if you're using MVVM) and another on asynchronous best practices (i.e., you should avoid async void).

这篇关于调用构造函数异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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