我有一個系結到一個的,CollectionView
它作業正常。但是當我在 XAML 中添加時,它沒有顯示任何專案,即使那些在 ViewModel 的建構式中初始化的專案也沒有顯示在. 它也不顯示更新的專案。ItemsSource
ObservableCollection
x:DataType="viewmodels:MyPageViewModel"
CollectionView
這是我的 XAML 代碼 (MyPage.xaml):
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
xmlns:viewmodels="clr-namespace:MyProject.ViewModels"
x:DataType="viewmodels:MyPageViewModel"
x:Class="MyProject.Views.MyPage">
<ContentPage.Content>
<StackLayout>
<CollectionView ItemsSource="{Binding Strings}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding .}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="Add More" Command="{Binding AddMoreItemsCommand}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
這里我在后面的 C# 代碼 (MyPage.xaml.cs) 中指定了 Binding:
BindingContext = new ViewModels.MyPageViewModel();
這是我的 ViewModel (MyPageViewModel.cs):
public class MyPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<string> Strings { get { return strings; } set { strings = value; OnPropertyChanged(nameof(Strings)); } }
ObservableCollection<string> strings;
public MyPageViewModel()
{
Strings = new ObservableCollection<string> { "1", "2", "3" };
}
public ICommand AddMoreItemsCommand => new Command(() =>
{
Strings.Add("4");
Strings.Add("5");
Strings.Add("6");
});
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
請注意,如果我洗掉x:DataType="viewmodels:MyPageViewModel"
,一切正常。此外,如果我在應用程式運行時編輯某些內容,它會通過XAML Hot ReloadColllectionView
按預期正確呈現專案。但它不能滿足需要,因為如果我再次重新運行專案,它就不起作用。
Xamarin Forms 中是否存在錯誤,我正在運行最新的 Xamarin.Forms 5.0.0.2515,我也在 Xamarin.Forms 5.0.0.2478 中嘗試過?還是我錯過了什么?任何幫助將非常感激。
uj5u.com熱心網友回復:
x:DataType="modelOfYourCollection"
在<DataTemplate>
中指定CollectionView
。
如果您正在使用任何具有 DataTemplate 的控制元件,則還必須在其上設定已編譯的系結。在這里,我們有一個CollectionView
資料系結到一個ObservableCollection<string>
. System
我們可以像之前那樣引入命名空間,viewmodels:MyPageViewModel
因為我們使用的是字串型別的集合,而 String 類駐留在 System 命名空間中。
- 首先,在頁面的根目錄中添加命名空間:
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
- 然后在您的
DataTemplate
添加中:
<DataTemplate x:DataType="sys:String">
如果我們使用自己的自定義模型,請在頁面的根目錄添加模型的命名空間,然后DataTemplate
在CollectionView
. 請注意,我們必須在DataTemplate
.
其余代碼正確
最終 XAML 代碼將如下所示:
<ContentPage ...
xmlns:viewmodels="clr-namespace:MyProject.ViewModels"
xmlns:models="clr-namespace:MyProject.Models"
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
x:DataType="viewmodels:MyPageViewModel"
x:Class="MyProject.Views.MyPage">
...
<CollectionView ItemsSource="{Binding Strings}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="sys:String">
<Label Text="{Binding .}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<!-- For collection of your custom model -->
<CollectionView ItemsSource="{Binding Monkeys}" >
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="models:MonkeyModel">
<Label Text="{Binding MonkeyName}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>
為什么使用編譯系結? 編譯的系結比經典系結更快地解決。因此,提高了 Xamarin.Forms 應用程式中的資料系結性能。
有關更多詳細資訊,請參閱 James Montemagno 的此博客。
請參閱此處以獲取 Xamarin.Forms 編譯系結官方檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506326.html
標籤:C# xml xamarin xamarin.forms 可观察的集合