简体   繁体   English

Xamarin Forms,使用async来应用ListView ItemSource

[英]Xamarin Forms, using async to apply ListView ItemSource

I am currently using Xamarin Forms and I am using the post method which I got from github RESTAPI . 我目前正在使用Xamarin Forms,我使用的是从github RESTAPI获得的post方法。 My application crashes whenever I try to apply the data to my ListView ItemsSource. 每当我尝试将数据应用到ListView ItemsSource时,我的应用程序崩溃。

The following is the success callback that is currently executed, and it retrieves the JSON and serialize it and storing it in a list called listData. 以下是当前执行的成功回调,它检索JSON并将其序列化并将其存储在名为listData的列表中。

public class Home : ContentPage
{
    private ListView myListView;
    private List<Person> listInfo = new List<Person> { };

    RESTAPI rest = new RESTAPI();
    Uri address = new Uri("http://localhost:6222/testBackEnd.aspx");

    public Home ()
    {
        Dictionary<String, String> data = new Dictionary<String, String>();
        rest.post(address, data, success, null);

        Content = new StackLayout { 
            Children = 
            {
                myListView
            }
        };
    }

    public void success(Stream stream)
    {
        DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(List<Person>));
        listData = (List<Person>)sr.ReadObject(stream);
        //myListView.ItemsSource = listData;
    }
}

Is it because it is not asynchronous, and if so how can I make this method asynchronous? 是因为它不是异步的,如果是这样,我怎么能使这个方法异步? I did one previously on Windows Phone 8.1 using the following await code, is there a Xamarin Forms equivalent available for it? 我之前在Windows Phone 8.1上使用以下等待代码做了一个,是否有可用的Xamarin Forms等效代码?

async public void success(Stream stream)
{
    DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(List<Person>));
    listData = (List<Person>)sr.ReadObject(stream);
    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        myListView.ItemsSource = listData;
    });
}

you can try with this way: 你可以尝试这种方式:

      Task.Run(()=> {
            downloadAllInformation();
             Device.BeginInvokeOnMainThread( ()=> {
                myListView.ItemSource = ... 
             });

         });

in this way you manage asynchronously the filling of listView. 通过这种方式,您可以异步管理listView的填充。

I've tried this with a lot of data and the performances are better. 我用很多数据试过这个,性能更好。

If it is because it is asynchronous, then you should do the UI change (always) in the main thread. 如果是因为它是异步的,那么你应该在主线程中进行UI更改(总是)。 To do so: 为此:

using Xamarin.Forms;
...
    Device.BeginInvokeOnMainThread(()=>{
        // do the UI change
    }

If this doesn't solve it, then async is not the (only?) problem. 如果这不能解决它,那么异步不是(唯一的?)问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Web服务方法分配给Xamarin表单中的ListView的ItemSource - Assign Web Service method to ItemSource of ListView in Xamarin Forms 尽管修改了 Itemsource,MVVM ListView 仍未更新 | XAMARIN.FORMS, ANDROID - MVVM ListView not updating despite Itemsource modification | XAMARIN FORMS, ANDROID 在Xamarin表单背后的代码中设置ListView ItemSource绑定 - Set ListView ItemSource Binding in Code Behind Xamarin Forms Xamarin 表单更新 listView itemSource - Xamarin form update listView itemSource ListView不会删除Xamarin.Forms中的项目。 我已将ObservableCollection分配给ListView itemsource。 MVVM - ListView not deleting items in Xamarin.Forms. I have assigned an ObservableCollection to the ListView itemsource. MVVM Xamarin.Forms (PRISM) - 将复选框绑定到 viewmodel 中的命令,在 listview itemsource 之外 - Xamarin.Forms (PRISM) - Bind checkbox, to a command in viewmodel, outside listview itemsource 从CodeBehind在DataTemplate内部绑定ListView ItemSource-Xamarin形式C# - Binding listview ItemSource inside DataTemplate from codebehind - xamarin forms c# Xamarin Forms - 调用事件“ItemSelected”时使自定义单元格绑定到原始列表视图项目源 - Xamarin Forms - Make custom cell bind to original listview itemsource when calling event "ItemSelected" ItemSource 外部的 Xamarin 表单绑定命令 - Xamarin Forms Bind command outside ItemSource Xamarin Forms iOS中的嵌套Bindabale布局ItemSource - Nested Bindabale Layout ItemSource in Xamarin Forms iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM