【问题标题】:display picker when clicking in imagebutton xamarin单击图像按钮 xamarin 时显示选择器
【发布时间】:2020-05-19 04:49:36
【问题描述】:

我想在单击 imageButton 时显示一个选择器(或列表),但它不起作用,但是当我只创建一个选择器时,它起作用了。

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);


          }

类设计模型

  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));               
   }
}}

我在行中有错误 stacklayout.Children.Add(p.ItemsSource); 在 p.itemsSource 下:无法从 'System.Collections.Ilist' 转换为 'Xamarin.Forms.View'

【问题讨论】:

  • 首先,你没有为 ImageButton 添加 tapGestureRecognizer,所以 tapGestureRecognizer.Tapped 永远不会触发,picker 无法显示。然后我尝试为 ImageButton 正确添加 TapGestureRecognizer,tapGestureRecognizer.Tapped 也无法触发,我猜可能是与点击事件冲突,你可以看到这个:github.com/xamarin/Xamarin.Forms/issues/4341,所以我建议你可以通过 imagebutton click 添加选择器事件。
  • 应该在哪里直观地显示 Picker,您的 Xaml /C#code 不显示它。它在 ImageButton 下面吗?如果是,ImageButton 的父视图是什么?
  • 我试图通过 imageButton click 事件添加选择器,但没有出现.. @CherryBu-MSFT

标签: xaml xamarin xamarin.forms imagebutton picker


【解决方案1】:

我尝试通过 imageButton 点击​​事件添加选择器,但没有出现。

首先,请确认EL.Data.DeviseList有list值,你有list绑定到Picker,然后将picker添加到当前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>

更新:

 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;
    }

【讨论】:

  • 你能看到出版物的更新吗?当我尝试做和你一样的事情时,我发现了一个错误,但是使用了一个服务(有列表)
  • @wafa 可以看到我将一个stacklayout命名为stacklayout,然后我想添加Picker来添加这个stacklayout,但是你想在stacklayout中添加Picker.itemsource,Picker.itemsource是列表集合,这是错误的。
  • 那我该怎么办?你能帮我吗!
  • @wafa 请尝试使用stacklayout.Children.Add(p)修改您的代码stacklayout.Children.Add(p.ItemsSource);
  • 它向我显示了一个列表,但它不是想要的列表,显示的是一个包含“NotesDesFrais.views.DeviseModel + DeviseL”的列表(这是 DeviseModel 类的路径)重复7 次。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
相关资源
最近更新 更多