作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正在使用 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/
我是一名优秀的程序员,十分优秀!