ans*_*lda 10 c# xamarin xamarin.forms
我正在使用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. " />
Run Code Online (Sandbox Code Playgroud)
类似地,我有办法在XAMARIN表单选择器中为每个国家/地区添加键值吗?
Mic*_*ahl 17
有一种方法可以使用数据绑定在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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
其次,你必须在Pickers定义中设置Pickers ItemsSource,ItemDisplayBinding和SelectedItem Bindings:
<Picker
ItemDisplayBinding="{Binding Value}"
ItemsSource="{Binding PickerItemList}"
SelectedItem="{Binding SelectedItem}" />
Run Code Online (Sandbox Code Playgroud)
鉴于此,您可以在View Model中获取所选项的键
SelectedItem.Key
Run Code Online (Sandbox Code Playgroud)
进一步阅读:https://developer.xamarin.com/guides/xamarin-forms/user-interface/picker/populating-itemssource/#Populating_a_Picker_with_Data_Using_Data_Binding
我刚刚面临同样的问题,我找到了一种方法来做到这一点。我只需要将选择器与元素列表绑定SomeClass。这是我所做的:
namespace MyApp.ViewModels
{
public class CboViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的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>
Run Code Online (Sandbox Code Playgroud)
不,关键属性在 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
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //developer.xamarin.com/api/type/Xamarin.Forms.Picker/
| 归档时间: |
|
| 查看次数: |
11326 次 |
| 最近记录: |