我正在嘗試顯示一些文本 - 或影像 - 如果ListView我的 Xamarin Forms 5 應用程式中沒有資料可供我顯示,但我無法讓它作業。
這是我的ListView樣子:
<ListView
x:Name="CustomersList"
ItemsSource="{Binding Customers, TargetNullValue='No customers!'}"
BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Text="{Binding FullName}"
TextColor="{StaticResource PrimaryDark}"
Command="{Binding Source={x:Reference Name=CustomersList}, Path=BindingContext.CustomerSelectedCommand}"
CommandParameter="{Binding .}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我的 ViewModel 中的源ListView設定如下
ObservableRangeCollection<CustomerModel> customers;
public ObservableRangeCollection<CustomerModel> Customers
{
get => customers;
set
{
if (customers == value)
return;
customers = value;
OnPropertyChanged();
}
}
如果我的 API 呼叫沒有回傳任何內容,我會執行Customers = null;
當我TargetNullValue如上所示使用時,我在系結TextCell到FullName. 表明:
在“N”上找不到“FullName”屬性,目標屬性:“Xamarin.Forms.TextCell.Text”
然后它對 . 中的每個字符重復此錯誤訊息No Customers!。
然后我嘗試使用FallbackValue. 這次我沒有收到錯誤,但它根本沒有顯示“沒有客戶!” 資訊。
如何處理ListView沒有資料可顯示?至少,我想展示一些簡單的短信,但最好展示一些更好的東西,也許是一張圖片。
uj5u.com熱心網友回復:
如果您可以從 to 切換ListView,CollectionView您可以使用內置的CollectionView.EmptyView:
<CollectionView ItemsSource="{Binding Customers}"
EmptyView="No customers!" ... />
同樣EmptyView,您可以定義任何視圖:
<CollectionView>
<CollectionView.EmptyView>
<ContentView>
<image ... />
</ContentView>
</CollectionView.EmptyView>
</CollectionView>
檔案:CollectionView EmptyView
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442267.html
