我正在嘗試回傳一個Observablecollection,似乎沒問題。當我想獲取集合中每個專案的屬性時,出現錯誤“系結:屬性''未找到”。
我有以下代碼:
我洗掉了所有標記代碼,以使其更易于閱讀
看法
<ListView ItemsSource="{Binding Books}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding Title}" />
<Label Text="{Binding Description}" />
<Label Text="{Binding Date}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
視圖模型
public class BookListPageViewModel : ViewModelBase
{
private ObservableCollection<Book> _books = new();
public ObservableCollection<Book> Books
{
get => _books;
set => SetProperty(ref _books, value);
}
}
模型
public class Book : ModelBase
{
public string Title { get; set; }
public string Description { get; set; }
public string Date { get; set; }
}
奇怪的是,早些時候我Book在ViewModel檔案中有這個類并且確實有效。但我希望這個類在Models檔案夾中。
這是我ViewModel以前的樣子。
namespace Some.Namespace.Project
{
public class Book : ModelBase
{
public string Title { get; set; }
public string Description { get; set; }
public string Date { get; set; }
}
public class BooksListPageViewModel : ViewModelBase
{
private ObservableCollection<Book> _books = new();
public ObservableCollection<Book> Books
{
get => _books;
set => SetProperty(ref _books, value);
}
}
}
它是如何的Bindings (Title, Description, and Date)都沒有發現?
uj5u.com熱心網友回復:
我是 XAML 的新手,顯然可以x-tags在ContentPage.
所以在我的 ContentPage 中,我添加了xmlns:models="clr-namespace:Namespace.To.Models".
有了這個標簽,我可以將我的更改DataTemplate為<DataTemplate x:DataType="models:Book">.
這為我解決了這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/399703.html
標籤:C# xaml xamarin.forms
