在 Xamarin.Forms 中,我有一个用于显示数据库项目的页面。
public ListItemsPage()
{
DatabaseHelper tmpDBHelper = new DatabaseHelper();
InitializeComponent();
listView.ItemsSource = tmpDBHelper.GetItems();
}
这些项目有 4 列:Id 为 Guid
、MandantId 为 Guid
、UpdateAt 为 DateTime
和 Url 为 String
.
现在我想像这样将它们绑定(bind)在我的 XAML 中。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Portable.Pages.ListItemsPage"
Title="DbItemExample">
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding="20,0,20,0"
Orientation="Horizontal"
HorizontalOptions="FillAndExpand">
<Label Text="{Binding Id}"
HorizontalOptions="StartAndExpand" />
<Label Text="{Binding Url}"
HorizontalOptions="StartAndExpand" />
<Label Text="{Binding MandantId}"
HorizontalOptions="StartAndExpand" />
<Label Text="{Binding UpdateAt}"
HorizontalOptions="StartAndExpand" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
如果这有帮助,这是我生成数据库表的模型。
class DbItem
{
[PrimaryKey]
public Guid Id { get; set; }
public Guid MandantId { get; set; }
public string Url { get; set; }
public DateTime UpdateAt { get; set; }
}
它只显示 Url 和 UpdateAt。这是为什么?
最佳答案
我修好了。
就像 Egor Gromadskiy 的建议一样,我实现了一个 EmptyConverter ( Binding errors in Xamarin Forms )。 Xamarin 似乎无法自动将 Guid 转换为字符串。
只需在方法 Convert()
中将 value
更改为 value.ToString()
即可。
关于c# - XAML 绑定(bind)到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489352/