【发布时间】:2016-08-29 09:34:26
【问题描述】:
我在读取sqlite时出现问题,即无法读取表中的数据(但读取的数据量),如下图:
XAML:
<ListBox x:Name="dictionary" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="20" Text="{Binding Id}" Visibility="Collapsed"/>
<TextBlock FontSize="20" Text="{Binding Word}" FontWeight="SemiBold" Margin="30,0,0,0"/>
<TextBlock FontSize="20" Text="{Binding Translation}" FontWeight="SemiBold" Margin="30,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
注意: 上面的 XAML 我用了 text = "Word" 和翻译 text = "Translation" 来测试 sqlite 上的数据是否清晰。虽然绑定数据的使用仍然不可读
代码:
public ObservableCollection<ind_dict> ReadIndo()
{
var sqlpath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, @"Assets\kamus.sqlite");
using (var dbConn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))
{
List<ind_dict> myCollection = dbConn.Table<ind_dict>().ToList<ind_dict>();
ObservableCollection<ind_dict> indoList = new ObservableCollection<ind_dict>(myCollection);
dictionary.ItemsSource = indoList;
return indoList;
}
}
ind_dict 类:
public class ind_dict
{
[SQLite.Net.Attributes.PrimaryKey]
public string Id { get; set; }
public string Word { get; set; }
public string Translation { get; set; }
public ind_dict(string word, string translation)
{
Word = word;
Translation = translation;
}
}
如何解决?
【问题讨论】:
-
使用贴出的代码有没有异常?
InstalledLocation是只读位置,处理SQLite文件最好放在LocalFolder这样的文件夹中。 -
不显示异常,只显示sqlite文件中的数据。结果如上所示。我正在尝试使用
和 进行测试。如果使用 和 ,显示的数据是空的。 -
我用你的代码在我身边进行了测试。但是我遇到了“System.MissingMethodException”,附加信息是
Constructor on type '<namespace>.ind_dict' not found.。我在ind_dict类中添加了一个默认构造函数,例如public ind_dict() { }来解决这个问题。在此之后,您的代码运行良好并显示数据。 -
我在 ind_dict 类中添加了一个构造函数,但是如果使用绑定,仍然无法显示数据。我包括项目:onedrive.live.com/…请帮我解决。
标签: c# sqlite xaml uwp win-universal-app