Xamarin 中的可绑定选择器
Bindable Picker in Xamarin
Xamarin 中的选择器控件是否可绑定?如果是这样,有人可以帮助说明如何使用它吗?我想将 Picker 控件与来自数据库的数据(XAML 方法)绑定。
是的,您可以在 Xamarin Forms 中使用 Picker 控件。
请检查此 link 的描述:https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/
谢谢。
可绑定选择器自 2017 年 1 月 13 日起可用。目前,它包含在版本 2.3.4.184-pre1 中。
如果你想使用它,你必须使用 -Pre
标志通过 nuget 安装 Xamarin.Forms。或者选中 nuget UI.
中的 Prerelease 复选框
Install-Package Xamarin.Forms -Pre
然后,您只需将 collection 绑定到 ItemsSource
。
<Picker
Title="Select a Color"
ItemsSource="{Binding Colors}" />
它将在 2017 年 2 月之前稳定发布(根据 Roadmap)
示例 #1:
使用xlabs
以下的Extendedpicker是link来实现extended picker
示例#2:
尝试创建自己的可弯曲拾取器参考以下 link
https://forums.xamarin.com/discussion/30801/xamarin-forms-bindable-picker
完整的解决方案如下:
https://hiranpeiris.com/2017/02/24/how-to-add-a-custom-bindable-property-to-xamarin-forms-control/
创建您的自定义选择器class。
using System.Collections.Generic;
using Xamarin.Forms;
namespace FDNet
{
public class OutletPicker : Picker
{
public static readonly BindableProperty ItemSourceProperty = BindableProperty.Create(nameof(ItemSource), typeof(List<string>), typeof(OutletPicker), null);
public List<string> ItemSource
{
get
{
return (List<string>)GetValue(ItemSourceProperty);
}
set
{
SetValue(ItemSourceProperty, value);
}
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == nameof(ItemSource))
{
this.Items.Clear();
if (ItemSource != null)
{
foreach (var item in ItemSource)
{
this.Items.Add(item);
}
}
}
}
}
}
向页面添加 XAML 引用变量。
xmlns:local=“clr–namespace:FDNet;assembly=FDNet“
添加控件并绑定属性.
<local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
现在你可以看到我们的自定义绑定属性 ItemSource=“{Binding Outlets}”)
演示。
Outlets = new List<string> { “4G LTE“, “4G Broadband“, “Fiber connection“ };
<local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
(1)绑定选择器:
词典dicobj=新词典();
dicobj.Add(1, "abc");
dicobj.Add(2, "xyz");
foreach (var item in dicobj.Values)
{
YorPickerName.Items.Add(item);
}
(2)然后在选取器的 SelectedIndexChanged 事件中获取选定值:
var selval = Service_category.SelectedIndex;
int value = dicobj.ElementAt(selval).Key;
var data = Service_category.Items[selval];
int id = dicval.FirstOrDefault(x => x.Value == data).Key;
Xamarin 中的选择器控件是否可绑定?如果是这样,有人可以帮助说明如何使用它吗?我想将 Picker 控件与来自数据库的数据(XAML 方法)绑定。
是的,您可以在 Xamarin Forms 中使用 Picker 控件。 请检查此 link 的描述:https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/
谢谢。
可绑定选择器自 2017 年 1 月 13 日起可用。目前,它包含在版本 2.3.4.184-pre1 中。
如果你想使用它,你必须使用 -Pre
标志通过 nuget 安装 Xamarin.Forms。或者选中 nuget UI.
Install-Package Xamarin.Forms -Pre
然后,您只需将 collection 绑定到 ItemsSource
。
<Picker
Title="Select a Color"
ItemsSource="{Binding Colors}" />
它将在 2017 年 2 月之前稳定发布(根据 Roadmap)
示例 #1:
使用xlabs
以下的Extendedpicker是link来实现extended picker
示例#2: 尝试创建自己的可弯曲拾取器参考以下 link https://forums.xamarin.com/discussion/30801/xamarin-forms-bindable-picker
完整的解决方案如下:
https://hiranpeiris.com/2017/02/24/how-to-add-a-custom-bindable-property-to-xamarin-forms-control/
创建您的自定义选择器class。
using System.Collections.Generic;
using Xamarin.Forms;
namespace FDNet
{
public class OutletPicker : Picker
{
public static readonly BindableProperty ItemSourceProperty = BindableProperty.Create(nameof(ItemSource), typeof(List<string>), typeof(OutletPicker), null);
public List<string> ItemSource
{
get
{
return (List<string>)GetValue(ItemSourceProperty);
}
set
{
SetValue(ItemSourceProperty, value);
}
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == nameof(ItemSource))
{
this.Items.Clear();
if (ItemSource != null)
{
foreach (var item in ItemSource)
{
this.Items.Add(item);
}
}
}
}
}
}
向页面添加 XAML 引用变量。
xmlns:local=“clr–namespace:FDNet;assembly=FDNet“
添加控件并绑定属性.
<local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
现在你可以看到我们的自定义绑定属性 ItemSource=“{Binding Outlets}”)
演示。
Outlets = new List<string> { “4G LTE“, “4G Broadband“, “Fiber connection“ }; <local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
(1)绑定选择器:
词典dicobj=新词典();
dicobj.Add(1, "abc");
dicobj.Add(2, "xyz");
foreach (var item in dicobj.Values)
{
YorPickerName.Items.Add(item);
}
(2)然后在选取器的 SelectedIndexChanged 事件中获取选定值:
var selval = Service_category.SelectedIndex;
int value = dicobj.ElementAt(selval).Key;
var data = Service_category.Items[selval];
int id = dicval.FirstOrDefault(x => x.Value == data).Key;