簡體   English   中英

如何將數據從 MySQL 數據庫綁定到 Xamarin ListView?

[英]How do I bind data from MySQL database to Xamarin ListView?

使用 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 文件,我想在其中將數據綁定到綁定

    <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。 其次,您需要創建一些類來對 db 表中的數據進行建模

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM