为什么我无法向数据库添加项目?

时间:2015-04-20 16:58:50

标签: c# xaml windows-phone-8.1 sqlite-net

我使用SQLite-net创建了一个数据库,然后以这种方式在其中插入一个对象:

SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "Database.db"), true);
await conn.CreateTableAsync<Musei>();

            Musei musPref;
            musPref = new Musei
                {
                    DidascaliaLista=muss.DidascaliaLista,
                    NomeMuseo = muss.NomeMuseo,
                    Luogopreciso = muss.Luogopreciso,
                    Descrizione = muss.Descrizione,
                    NumeroTel = muss.NumeroTel,
                    IndirizzoEmail = muss.IndirizzoEmail,
                    Immagine= muss.Immagine,
                };



await conn.InsertAsync(musPref);

在新页面中,我想在DB

中的ListView元素中检索数据库节目

这是代码c#:

SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "Database.db"), true);
await conn.CreateTableAsync<Musei>();
var Dbase = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Database.db");
var con = new SQLiteAsyncConnection(Dbase, true);

var query = await con.Table<Musei>().ToListAsync();
ListaPreferiti.Source = query;

这是代码XAML:

<Page.Resource>
<CollectionViewSource x:Name="ListaPreferiti" IsSourceGrouped="True"/>
</Page.Resource>
.....
<ListView x:Name="ListPreferiti" ItemsSource="{Binding Source={StaticResource ListaPreferiti}}" IsItemClickEnabled="True" ItemClick="ListPreferiti_ItemClick" Margin="0,-1,0.667,-0.333">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="15"/>
                                                <ColumnDefinition Width="5"/>
                                                <ColumnDefinition Width="30"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="30"/>
                                                <RowDefinition Height="30"/>
                                                <RowDefinition Height="30"/>
                                                <RowDefinition Height="10"/>
                                            </Grid.RowDefinitions>

                                            <Image x:Name="Cerchio" Grid.Column="0" Grid.Row="0" Source="Assets/120.png" Width="15" Height="15" VerticalAlignment="Top" Margin="0,13,0,0"/>

                                            <TextBlock Grid.Column="3" Grid.Row="0" Text="{Binding NomeMuseo}" TextWrapping="Wrap" FontSize="24" FontWeight="Bold"/>
                                            <TextBlock Grid.Column="3" Grid.Row="2" Text="{Binding Descrizione}" TextWrapping="Wrap" FontSize="18"/>
                                            <Image Grid.Column="2" Grid.Row="0" Source="Assets/NoImage.png"/>
                                            <TextBlock Grid.Column="3" Grid.Row="1" FontSize="18" Text="{Binding Luogopreciso}"/>
                                        </Grid>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                                <ListView.GroupStyle>
                                    <GroupStyle HidesIfEmpty="True">
                                        <GroupStyle.HeaderTemplate>
                                            <DataTemplate>
                                                <Grid Background="Chocolate">
                                                    <TextBlock Text="{Binding Key}" Foreground="Beige" 
                                                   Style="{StaticResource HeaderTextBlockStyle}"/>
                                                </Grid>
                                            </DataTemplate>
                                        </GroupStyle.HeaderTemplate>
                                    </GroupStyle>
                                </ListView.GroupStyle>
                            </ListView>
                        </PivotItem>
                    </Pivot>
                </Grid>
            </PivotItem>
        </Pivot>

我无法理解为什么ListView为空。只需尝试启动应用程序,Debug Visual Studio的输出我看到这条消息:

&#34;错误:BindingExpression路径错误:&#39;键&#39;在App1.Musei,App1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#39;上找不到属性。 BindingExpression:Path =&#39; Key&#39; DataItem =&#39; App1.Musei,App1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#39 ;;目标元素是&#39; Windows.UI.Xaml.Controls.TextBlock&#39; (名称=&#39;空&#39);目标属性是&#39; Text&#39; (键入&#39; String&#39;)&#34;

0 个答案:

没有答案