绑定到标签

本文关键字:标签 绑定 | 更新日期: 2023-09-27 18:07:02

In Xamarin。我有一个显示数据库项的页面。

public ListItemsPage()
{
    DatabaseHelper tmpDBHelper = new DatabaseHelper();
    InitializeComponent();
    listView.ItemsSource = tmpDBHelper.GetItems();
}

这些项有4列:Id为Guid, MandantId为Guid, UpdateAt为DateTime, Url为String

现在我想在我的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>

如果有帮助,下面是我的模型,其中db表是生成的。

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 (Xamarin Forms中的绑定错误)。Xamarin似乎不能自动将Guid转换为字符串。

只要在Convert()方法中将value更改为value.ToString()就可以了。