2017-06-16 20 views
0

我开始学习Xamarin,并且正在尝试创建第一个测试项目。我创建了ListView,它显示BindingContext中的ObservableCollection。 “Take”是一个有三个属性的表格。问题是,当我在仿真器上运行应用程序时,出现以下的错误对话框:“过程系统没有响应,你想关闭它吗?”
但是,如果我擦除标记和所有内部XAML代码一切正常,但我想在ListView的每个元素的类“Take”类的属性的值。Xamarin中的XAML ListView不起作用

<?xml version="1.0" encoding="utf-8" ?> 

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="App1.Views.Page1"> 

<ContentPage.Content> 
    <StackLayout> 
     <Label Text="Welcome to my first project"></Label> 
     <Label Text="Why does it does not work?"></Label> 
     <ListView ItemsSource="{Binding GetList}"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <TextCell Text="{Binding Word}"></TextCell> 
        <Button Text="SomeText"></Button> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 
</ContentPage.Content> 
</ContentPage> 

这是我的C#的BindingContext

public class SQLiteSamplePage 
{ 
    private readonly SQLiteConnection _sqLiteConnection; 
    private ObservableCollection<Take> list; 

    public SQLiteSamplePage() 
    { 
     _sqLiteConnection = DependencyService.Get<ISQLite>().GetConnection(); 
     _sqLiteConnection.CreateTable<Take>(); 
     _sqLiteConnection.Insert(new Take 
     { 
      Word = "SomeWord1", 
      Translation = "Some translation 1" 
     }); 

     _sqLiteConnection.Insert(new Take 
     { 
      Word = "SomeWord", 
      Translation = "SomeTranslation" 
     }); 

     list =new ObservableCollection<Take>(_sqLiteConnection.Table<Take>()); 
    } 
    public ObservableCollection<Take> GetList 
    { 
     get { return list; } 
    } 
} 

这里是一个表

public class Table 
{ 
    [PrimaryKey, AutoIncrement] 
    public int ID { get; set; } 
    public string Word { get; set; } 
    public string Translation { get; set; } 
} 
+0

尝试从您的DataTemplate中删除按钮 – Jason

回答

0

尝试的代码把TextCell并在StackLayoutButton或任何种类的内部布局<ViewCell>元素:

<ListView ItemsSource="{Binding GetList}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
       <StackLayout> 
        <TextCell Text="{Binding Word}"></TextCell> 
        <Button Text="SomeText"></Button> 
       </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

DataTemplate元素的子元素必须是ViewCell类型的元素或派生自ViewCell类型的子元素。

0

你不能合并TextCell和其他东西,在你的情况下,一个Button。如果你想显示文本和按钮,你将需要使用自定义单元格。

这是使用基类ViewCell完成,里面有你定义要显示

<StackLayout> 
    <Label Text="Welcome to my first project"></Label> 
    <Label Text="Why does it does not work?"></Label> 
    <ListView ItemsSource="{Binding GetList}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <ViewCell> 
        <StackLayout> 
         <Label Text="{Binding Word}"></Label> 
         <Button Text="SomeText"></Button> 
        </StackLayout> 
       </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</StackLayout> 

希望这有助于布局。