question

Julien-2587 avatar image
0 Votes"
Julien-2587 asked JarvanZhang-MSFT commented

Display an image in a ListView

Hello, I have a Prestashop database and an app that connects to it. With queries, I can display the data on images for example, but I would like to display the image instead of the data. I have defined a ListView there to display the results but only the image information is saved in the database.

126351-ecran-id-product.png



My Recherche.xaml.cs

 private void VoirImages(object sender, EventArgs e)
         {
             if (afficherImages.Text == "Voir les images")
             {
                 cn = new MySqlConnection("***************");
                 try
                 {
                     if (cn.State == System.Data.ConnectionState.Closed) { cn.Open(); }
                     DisplayAlert("Infos:", "Vous êtes connecté", "ok");
                     afficherImages.Text = "Se déconnecter";
                     Connecté = true;
                     imagesbdd.IsVisible = true;
                     produitsbdd.IsVisible = false;
                     tracteursbdd.IsVisible = false;
                     requettebdd.IsVisible = false;
    
                     {
                         if (Connecté)
                         {
                             MySqlCommand cmd = new MySqlCommand("SELECT * FROM ps_image_lang WHERE id_image = 24192", cn);
                             using (MySqlDataReader Lire = cmd.ExecuteReader())
                             {
                                 var data = new List<Image>();
                                 while (Lire.Read())
                                 {
                                     string id_image = Lire["id_image"].ToString();
                                     string id_lang = Lire["id_lang"].ToString();
                                     string legend = Lire["legend"].ToString();
                                     data.Add(new Image(id_image,id_lang ,legend));
                                 }
                                 imagesbdd.ItemsSource = data;
                             }
                         }
                         else
                         {
                             DisplayAlert("Infos:", "Plantage", "Ok");
                         }
                     }
                 }
                 catch (Exception exp) { DisplayAlert("Info:", "Plantage", "ok"); }
             }
             else
             {
                 cn.Close();
                 DisplayAlert("Infos:", "Vous êtes déconnecté", "ok");
                 afficherImages.Text = "Voir les images";
                 imagesbdd.IsVisible = false;
                 produitsbdd.IsVisible = false;
                 tracteursbdd.IsVisible = false;
                 requettebdd.IsVisible = false;
             }
         }

My Recherche.xaml

 <ListView x:Name="imagesbdd"
                       IsVisible="false"
                       SeparatorColor="DeepSkyBlue"
                       HorizontalScrollBarVisibility="Always"
                       HorizontalOptions="Start"
                       VerticalOptions="Start">
                 <ListView.ItemsSource>
                     <x:Array Type="{x:Type x:String}">
                         <x:String>Liste des images</x:String>
                     </x:Array>
                 </ListView.ItemsSource>
             </ListView>

My Image.cs

 namespace TractoApp.Data
 {
     class Image
     {
         public string id_image;
         public string id_lang;
         public string legend;
    
         public string Id_image()
         {
             return id_image;
         }
         public string Id_lang()
         {
             return id_lang;
         }
         public string Legend ()
         {
             return legend;
         }
    
         public Image(string id_image, string id_lang, string legend)
         {
             this.id_image = id_image;
             this.id_lang = id_lang;
             this.legend = legend;
         }
         public override string ToString()
         {
             return "N° " + id_image  + " - " + legend;
         }
     }
    
 }

How can I convert the id to the address of the image to display it?

dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Julien-2587. To load images from the Prestashop database, you needs to get the image urls first. For this, please check this similar issue case. https://stackoverflow.com/questions/24683386/get-image-url-in-prestashop

Then get the image urls in xamarin project and set binding to the Image view.

<ViewCell>
    <StackLayout>
        <Label Text="{Binding Title}"/>
        <Image Source="{Binding ImgSource}" HeightRequest="35"/>
    </StackLayout>
</ViewCell>
0 Votes 0 ·

Thank you for the reply, I am looking at all this in detail and come back if necessary.

0 Votes 0 ·

Waiting for your update.

0 Votes 0 ·

0 Answers