我想在 Xamarin 表單的 ListView 中顯示資料。
當我打開 ListViewPage 時,我從我的 API 中獲得一組專案 (RückmeldeItems),這取決于唯一的 ID。
我希望 ListView 只顯示新的專案集。
當前,它顯示頁面第一次出現時已加載的專案。當我重新加載它時,我希望 ListView 包含新的專案集并洗掉舊集。
我嘗試通過將新的 List<RückmeldeItems> 分配給 ListView 的 BindingContext 來實作它,但這不起作用。此錯誤記錄在Microsoft Docs for ListViews
我也嘗試過 Clear() 它,然后使用 Add(rückmeldeItem) 一個一個地分配元素,但無濟于事。
如何使 ListView 包含正確的專案集?
內容系結到的 IList:
public IList<RückmeldeItem> rückmeldeItems { get; private set; }
用于填充 ListView aka BindingContext 的方法(我評論了不作業的嘗試):
private void FillListView(List<RückmeldeItem> rückmeldeItemsList)
{
if(rückmeldeItems == null)
{
rückmeldeItems = rückmeldeItemsList;
}
//rückmeldeItems.Clear();
/*foreach (RückmeldeItem rückmeldeItem in rückmeldeItemsList)
{
rückmeldeItems.Add(rückmeldeItem);
//DisplayAlert(rückmeldeItem.RückmeldeNummer.ToString(), rückmeldeItem.Status, "OK");
}*/
BindingContext = this;
}
獲取 RückmeldeItems 的方法(為監督而簡化,但它回傳一個作業 List<RückmeldeItem>)
private async Task<List<RückmeldeItem>> GetRNRs()
{
List<RückmeldeItem> rückmeldeItemsList = ...the code fetching the RückmeldeItems...;
return rückmeldeItemsList;
}
每當頁面出現時呼叫的方法。呼叫其他方法:
private async void EventPage_OnAppearing(object sender, EventArgs e)
{
List<RückmeldeItem> rückmeldenummern = await GetRNRs();
FillListView(rückmeldenummern);
}
編輯:我想以自定義樣式顯示 3 個屬性。最好的方法似乎是使用資料系結,但是我得到了手動設定 ItemsSource 的建議。
手動設定源時如何保留此自定義布局?
有問題的xaml:
<ListView x:Name="ListViewRückmeldenummern" ItemTapped="ListViewRückmeldenummern_ItemTapped" RowHeight="80" ItemsSource="{Binding rückmeldeItems}" VerticalScrollBarVisibility="Always">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid >
<Label Text="{Binding RückmeldeNummer}" Grid.Row="0" Grid.RowSpan="2" TextColor="Black" FontSize="Large" FontFamily="Hansom" Grid.Column="0" Margin="10,15,0,15"/>
<Label Text="{Binding Artikelbezeichnung}" Grid.Row="0" TextColor="#888888" FontSize="Small" FontFamily="Hansom" Grid.Column="1" Margin="0,15,10,0" HorizontalTextAlignment="End"/>
<Label Text="{Binding Status}" Grid.Row="1" TextColor="#888888" FontSize="Small" FontFamily="Hansom" Grid.Column="1" Margin="0,0,10,15" HorizontalTextAlignment="End"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
uj5u.com熱心網友回復:
手動設定 ItemsSource 使其作業!我自己回答了這個問題,所以我可以在 3 天內結束這個問題。學分去杰森。
新方法:
private void FillListView(ObservableCollection<RückmeldeItem> rückmeldeItemsList)
{
ListViewRückmeldenummern.ItemsSource = rückmeldeItemsList;
BindingContext = this;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511554.html
