2016-09-14 97 views
2

在Xamarin.Forms中我有一个显示数据库项目的页面。XAML绑定到标签

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> 

如果这有帮助,这是我的模型的数据库表生成。

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。这是为什么?

+0

'Url'和'MandantId'来自数据库吗? –

+0

@EgorGromadskiy是的,他们这样做。我获得了具有所有属性的cs文件中的项目。 – greenhoorn

+0

我会添加'EmptyConverter'并会查看绑定是否有效。 –

回答

1

我修好了。

像Egor Gromadskiy建议的那样,我实现了EmptyConverter(Binding errors in Xamarin Forms)。看起来Xamarin不能自动将Guid转换为字符串。

只需将方法Convert()中的value更改为value.ToString()即可。

+0

_它只显示Id_ - 但'Id'也是'Guid'。那么为什么要显示“Id”? –

+0

@EgorGromadskiy对不起,错误,它显示Url和UpdateAt。仍然奇怪它在DateTime上调用ToString(),但不在Guid上。 – greenhoorn