【发布时间】: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