2016-09-14 147 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