我正在使用XAMARIN选择器来选择国家/地区。这些国家在选择器中进行了硬编码。有没有办法通过键值识别每个国家/地区名称。我使用SAPUI5以类似的方式完成了这项工作。
<core:Item key="AF" text="Afghanistan " />
<core:Item key="AL" text="Albania " />
<core:Item key="DZ" text="Algeria " />
<core:Item key="VI" text="Amer.Virgin Is. " />
类似地,我有办法在XAMARIN表单选择器中为每个国家/地区添加键值吗?
答案 0 :(得分:15)
有一种方法可以使用数据绑定在Picker中使用Key-Value-Pairs。
首先,您必须在表单的视图模型中定义字典,并定义一个返回字典键值对列表的属性。此外,还需要绑定到当前选定的项目:
class MyViewModel
{
...
private Dictionary<string, string> PickerItems =
new Dictionary<string, string>() { {"AF", "Afghanistan"}, {"AL", "Albania" } };
public List<KeyValuePair<string, string>> PickerItemList
{
get => PickerItems.ToList();
}
private KeyValuePair<string, string> _selectedItem;
public KeyValuePair<string, string> SelectedItem
{
get => _selectedItem;
set => _selectedItem = value;
}
...
}
其次,你必须在Pickers定义中设置Pickers ItemsSource,ItemDisplayBinding和SelectedItem Bindings:
<Picker
ItemDisplayBinding="{Binding Value}"
ItemsSource="{Binding PickerItemList}"
SelectedItem="{Binding SelectedItem}" />
鉴于此,您可以通过
获取视图模型中所选项目的键SelectedItem.Key
答案 1 :(得分:4)
不,关键属性在xamarin选取器中可用。但是,您可以使用Xamarin选取器类的Dictionary类和SelectedIndex属性来实现它。
使用以下代码段实现它:
class PickerDemoPage : ContentPage
{
// Dictionary to get Color from color name.
Dictionary<string, Color> nameToColor = new Dictionary<string, Color>
{
{ "Aqua", Color.Aqua }, { "Black", Color.Black },
{ "Blue", Color.Blue }, { "Fuschia", Color.Fuschia },
{ "Gray", Color.Gray }, { "Green", Color.Green },
{ "Lime", Color.Lime }, { "Maroon", Color.Maroon },
{ "Navy", Color.Navy }, { "Olive", Color.Olive },
{ "Purple", Color.Purple }, { "Red", Color.Red },
{ "Silver", Color.Silver }, { "Teal", Color.Teal },
{ "White", Color.White }, { "Yellow", Color.Yellow }
};
public PickerDemoPage()
{
Label header = new Label
{
Text = "Picker",
FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
};
Picker picker = new Picker
{
Title = "Color",
VerticalOptions = LayoutOptions.CenterAndExpand
};
foreach (string colorName in nameToColor.Keys)
{
picker.Items.Add(colorName);
}
// Create BoxView for displaying picked Color
BoxView boxView = new BoxView
{
WidthRequest = 150,
HeightRequest = 150,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
};
picker.SelectedIndexChanged += (sender, args) =>
{
if (picker.SelectedIndex == -1)
{
boxView.Color = Color.Default;
}
else
{
string colorName = picker.Items[picker.SelectedIndex];
boxView.Color = nameToColor[colorName];
}
};
// Accomodate iPhone status bar.
this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
// Build the page.
this.Content = new StackLayout
{
Children =
{
header,
picker,
boxView
}
};
}
}
来源:https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/
答案 2 :(得分:2)
我正面临着同样的问题,我找到了解决方法。我只需要将选择器与SomeClass
元素列表绑定即可。这是我所做的:
namespace MyApp.ViewModels
{
public class CboViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
}
然后在我的XAML
文件中:
<ContentPage ...
xmlns:vm="clr-namespace:MyApp.ViewModels;assembly=Myapp" >
<ContentPage.Content>
...
<Picker x:Name="pckStatus" HorizontalOptions="FillAndExpand" >
<Picker.ItemsSource>
<x:Array Type="{x:Type vm:CboViewModel}" >
<vm:CboViewModel Id="0" Name="All" />
<vm:CboViewModel Id="1" Name="New" />
<vm:CboViewModel Id="2" Name="Standby" />
<vm:CboViewModel Id="4" Name="In Progress" />
<vm:CboViewModel Id="8" Name="Submitted" />
<vm:CboViewModel Id="16" Name="Closed" />
</x:Array>
</Picker.ItemsSource>
<Picker.ItemDisplayBinding>
<Binding Path="Name" />
</Picker.ItemDisplayBinding>
<Picker.SelectedIndex>0</Picker.SelectedIndex>
</Picker>
...
<ContentPage.Content>
</ContentPage>