Xamarin Forms,使用异步来应用 ListView ItemSource
Xamarin Forms, using async to apply ListView ItemSource
我目前正在使用 Xamarin Forms,我正在使用从 github RESTAPI 获得的 post
方法。每当我尝试将数据应用到我的 ListView ItemsSource 时,我的应用程序就会崩溃。
以下是当前执行的成功回调,它检索 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;
}
}
是不是因为它不是异步的,如果是的话我怎样才能使这个方法异步?我之前在 Windows Phone 8.1 上使用以下 await 代码做了一个,是否有等效的 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;
});
}
如果是因为它是异步的,那么您应该(总是)在主线程中进行 UI 更改。
为此:
using Xamarin.Forms;
...
Device.BeginInvokeOnMainThread(()=>{
// do the UI change
}
如果这不能解决问题,那么异步不是(唯一?)问题。
你可以这样试试:
Task.Run(()=> {
downloadAllInformation();
Device.BeginInvokeOnMainThread( ()=> {
myListView.ItemSource = ...
});
});
通过这种方式,您可以异步管理 listView 的填充。
我已经用大量数据试过了,性能更好。
我目前正在使用 Xamarin Forms,我正在使用从 github RESTAPI 获得的 post
方法。每当我尝试将数据应用到我的 ListView ItemsSource 时,我的应用程序就会崩溃。
以下是当前执行的成功回调,它检索 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;
}
}
是不是因为它不是异步的,如果是的话我怎样才能使这个方法异步?我之前在 Windows Phone 8.1 上使用以下 await 代码做了一个,是否有等效的 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;
});
}
如果是因为它是异步的,那么您应该(总是)在主线程中进行 UI 更改。 为此:
using Xamarin.Forms;
...
Device.BeginInvokeOnMainThread(()=>{
// do the UI change
}
如果这不能解决问题,那么异步不是(唯一?)问题。
你可以这样试试:
Task.Run(()=> {
downloadAllInformation();
Device.BeginInvokeOnMainThread( ()=> {
myListView.ItemSource = ...
});
});
通过这种方式,您可以异步管理 listView 的填充。
我已经用大量数据试过了,性能更好。