Search code examples
xamlxamarinxamarin.formsimagebuttonpicker

display picker when clicking in imagebutton xamarin


i want to display a picker (or list) when i click in my imageButton but it didn't work, but when i create only a picker , it worked.

Xaml

     <StackLayout Orientation="Horizontal" x:Name="stacklayout">
                <Entry Placeholder="préciser l'entité correspondante" 
                       ClearButtonVisibility="WhileEditing" x:Name="entit"/>

                <ImageButton Source="list.png" WidthRequest="30" HeightRequest="30" x:Name="listEntité" Clicked="listEntité_Clicked"/>

Xaml.cs

 private void listEntité_Clicked(object sender, EventArgs e)
    {

               Picker p = new Picker();
              HttpClient httpClient = new HttpClient();
              httpClient.BaseAddress = new Uri("http://192.168.1.3:3000/api/adepApi/GetCurrencyLists");
              httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
              HttpResponseMessage response = await httpClient.GetAsync("http://192.168.1.3:3000/api/adepApi/GetCurrencyLists");
              var content = await response.Content.ReadAsStringAsync();
              ResponseDataD EL = JsonConvert.DeserializeObject<ResponseDataD>(content);
              p.ItemsSource= EL.Data.DeviseList;
              stacklayout.Children.Add(p.ItemsSource);


          }

class DeviseModel

  public class DeviseModel
{
    public class DeviseL
    {
     //   public string devis;
        [JsonProperty("Label")]

        public string Devis { get; set; }
        [JsonProperty("Value")]

        public int id { get; set; }


    }
    public class ResponseDataD
    {
        public RootModelDevise Data;
    }
    public class RootModelDevise : INotifyPropertyChanged
    {

        List<DeviseL> deviseList;

        [JsonProperty("list")]
        public List<DeviseL> DeviseList
        {
            get { return deviseList; }
            set
            {
                if (deviseList != value)
                {
                    deviseList = value;
                    OnPropertyChanged();
                }
            }
        }
        DeviseL itemDevise;
        public DeviseL ItemDevise
        {
            get { return itemDevise; }
            set
            {
                if (itemDevise != value)
                {
                    itemDevise = value;
                    OnPropertyChanged();
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {

            var changed = PropertyChanged;
            if (changed == null)
                return;
            changed.Invoke(this, new PropertyChangedEventArgs(propertyName));               
   }
}}

i have an error in the line stacklayout.Children.Add(p.ItemsSource); under p.itemsSource :unable to convert from 'System.Collections.Ilist' to 'Xamarin.Forms.View'


Solution

  • i have tried to add picker by imageButton click event , but nothing appears.

    Firstly, please confirm that the EL.Data.DeviseList have list value, you have list binding to Picker, then adding picker to current contentPage.

    public partial class Page16 : ContentPage
    {
        private List<string> list;
        public Page16()
        {
            InitializeComponent();
            list = new List<string>();
    
            list.Add("test 1");
            list.Add("test 2");
            list.Add("test 3");
            list.Add("test 4");
    
        }
    
        private void imagebutton1_Clicked(object sender, EventArgs e)
        {
            Picker p = new Picker();
            p.ItemsSource = list;
            stacklayout.Children.Add(p);
        }
    }
    
     <StackLayout x:Name="stacklayout">
            <ImageButton
                x:Name="imagebutton1"
                Clicked="imagebutton1_Clicked"
                HeightRequest="30"
                Source="plu3.png"
                WidthRequest="30" />
    
        </StackLayout>
    

    Update:

     private void imagebutton1_Clicked(object sender, EventArgs e)
        {
            Picker p = new Picker();
            p.SelectedIndexChanged += P_SelectedIndexChanged;
            p.ItemsSource = devs;     
            p.ItemDisplayBinding = new Binding("Devis");
    
            stacklayout.Children.Add(p);
        }
    
        private void P_SelectedIndexChanged(object sender, EventArgs e)
        {
            Picker p = sender as Picker;
            DeviseL selectitem = p.SelectedItem as DeviseL;
            entit.Text =selectitem.Devis;
        }