gpt4 book ai didi

c# - 如何将 MySQL 数据库中的数据绑定(bind)到 Xamarin ListView?

转载 作者:行者123 更新时间:2023-11-29 09:24:05 32 4
gpt4 key购买 nike

正在使用 xamarin 和在线 MySQL 数据库开发学校项目,但遇到了麻烦。这是我的 teme.cs,我不知道如何编写读取器代码,以便它将读取数据库中的所有行

MySqlConnection con = new MySqlConnection("server=xxx; database=xxx; uid=xxx; pwd=xxxx"); try {

if (con.State == ConnectionState.Closed)
{
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM teme");
cmd.Connection = con;
cmd.ExecuteNonQuery();

} }

这是我的 teme.xaml 文件,我想在其中将数据绑定(bind)到绑定(bind)

    <ContentPage.Content>
<ListView HasUnevenRows="True" x:Name="temeList" SeparatorColor="#B6B6B6">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Text="Naslov:"></Label>
<Label Grid.Column="1"
Text="{Binding naslov}"
FontAttributes="Bold" FontSize="20" />
<Label Grid.Row="1" Text="Mentorji:"></Label>
<Frame Padding="5" Grid.Column="1" Grid.Row="1" BackgroundColor="#A1CBD9" CornerRadius="8" HorizontalOptions="Start">
<Label Grid.Row="1" Grid.Column="1" x:Name="mentorji" VerticalOptions="End" />
</Frame>
<Label Grid.Row="2" Text="Zasedenost:"></Label>
<Frame Padding="5" Grid.Column="1" Grid.Row="2" BackgroundColor="#d9534f" CornerRadius="8" HorizontalOptions="Start">
<Label Grid.Row="4" Grid.Column="1" VerticalOptions="End">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="{Binding zasedenost}" />
<Span Text="/"></Span>
<Span Text="{Binding max}"></Span>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
</Frame>
<Label Grid.Row="3" Text="Prostor:"></Label>
<Frame Grid.Column="1" Padding="5" Grid.Row="3" CornerRadius="8" BackgroundColor="#5bc0de" HorizontalOptions="Start">
<Label Grid.Row="4"
Grid.Column="1"
Text="{Binding prostor}"
VerticalOptions="End" />
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>

最佳答案

首先,不要使用 ExecuteNonQuery。其次,您需要创建一些类来对数据库表中的数据进行建模

List<MyClass> data = new List<MyClass>();

MySqlDataReader rdr = cmd.ExecuteReader();

while (rdr.Read())
{
var item = new MyClass();
// here you will need to set the properties of item
// to the various columns of your DB
data.Add(item);
}
rdr.Close();

// set the ItemsSource of your ListView
temeList.ItemsSource = data;

关于c# - 如何将 MySQL 数据库中的数据绑定(bind)到 Xamarin ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59920724/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com