我的Xamarin Forms应用程序中有一个列表,我已将其绑定到数据。所有这些都有效,我知道我正在获取数据,因为如果我点击屏幕上的列表会显示数据,但是我认为列表应该显示每个项目,而无需点击屏幕。
这是我的xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PDInstall.Views.LayoutsPage"
Title="Layouts">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="layoutsList" IsPullToRefreshEnabled="True"
Refreshing="layoutsList_Refreshing" ItemTapped="layoutsList_ItemTappedAsync">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
这是我后面的代码:
public LayoutsPage (Job job)
{
InitializeComponent ();
_job = job;
Task.Run(() => GetLayoutsListAsync());
}
private async Task GetLayoutsListAsync()
{
if(CrossConnectivity.Current.IsConnected)
{
layoutsList.IsRefreshing = true;
IList<CloudBlockBlob> layouts = await azureFileUpload.ListJobBlobsAsync(_job.JobId);
IList<CloudEditableBlob> layoutList = new List<CloudEditableBlob>();
foreach(var item in layouts)
{
CloudEditableBlob editableBlob = new CloudEditableBlob();
editableBlob.Name = item.Name.Replace(_job.JobId.ToString() + ": ", "");
editableBlob.Container = item.Container;
editableBlob.Parent = item.Parent;
editableBlob.StorageUri = item.StorageUri;
editableBlob.Uri = item.Uri;
layoutList.Add(editableBlob);
}
layoutsList.IsRefreshing = false;
await Task.Run(() => layoutsList.ItemsSource = layoutList);
}
else
{
await DisplayAlert("No Internet Connection", "Please Reconnect to the Internet and re-open PDInstall", "OK");
}
layoutsList.EndRefresh();
}
我正在从azure中获取blob列表,它们只是我们存储的PDF。我得到了Blob列表,然后必须将其放入自定义类中,以便可以更改名称,然后将其放入列表中。一切正常,直到尝试显示该列表,然后我得到一个空白页,直到我点击它,然后显示列表。
有人知道我是否可以以编程方式点击或显示该列表吗?或者可能是另一种无需点击屏幕即可显示数据的方法?
谢谢!
答案 0 :(得分:1)
替换
await Task.Run(() => layoutsList.ItemsSource = layoutList);
使用
Device.BeginInvokeOnMainThread(() =>{ layoutsList.ItemsSource = layoutList; });