如何将字典键对放入 xamarin 表单的选择器中并从选择器中检索键值?
How do I put a dictionary key pair into a picker in xamarin forms and retrieve the key value from the picker?
我有一个 xaml 文件,我在其中放置了选择器和一个用于填充它的代码。下面使用的代码:
Dictionary<string, int> list = new Dictionary<string, int>
{
{ "Aqua", 6},
{ "Red", 1 },
{ "Silver", 2 },
{ "Teal", 3 },
{ "White", 4 },
{ "Yellow", 55 }
};
foreach (string item in list.Keys)
{
ddlPurpose.Items.Add(item);
}
我试图在 select 黄色时获取值 55,但我唯一得到的是 5。我正在使用它来获取 selected 值
var val1 = ddlPurpose.SelectedItem.ToString();
var val2 = ddlPurpose.SelectedIndex;
难道连键值都可以拿到?已经查看了 BindablePicker,但这似乎根本不起作用。非常感谢对此的任何帮助。
我猜你的意思是:
var pSelectedIndex = ddlPurpose.SelectedIndex;
var selectedKey = list.Values.ElementAt(pSelectedIndex);
我建议您熟悉 MVVM,在这种特定情况下,我建议您熟悉 Behaviors。我写了一个小例子来演示使用 MVVM 的样子:
public class PickerKeyValueTestViewModel : INotifyPropertyChanged
{
static Dictionary<string, int> colors { get; } = new Dictionary<string, int>
{
{ "Aqua", 6 },
{ "Red", 1 },
{ "Silver", 2 },
{ "Teal", 3 },
{ "White", 4 },
{ "Yellow", 55 }
};
public List<string> Colors { get; } = colors.Keys.ToList();
public string SelectedColor { get; set; }
public void OnSelectedColorChanged()
{
if (string.IsNullOrEmpty(SelectedColor)) return;
var selectedValue = colors[SelectedColor];
}
// Using PropertyChanged.Fody
public event PropertyChangedEventHandler PropertyChanged;
}
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PickerKeyValueTest"
x:Class="PickerKeyValueTest.PickerKeyValueTestPage">
<ContentPage.BindingContext>
<local:PickerKeyValueTestViewModel />
</ContentPage.BindingContext>
<StackLayout
Margin="25">
<Picker
ItemsSource="{Binding Colors}"
SelectedItem="{Binding SelectedColor}">
</Picker>
</StackLayout>
</ContentPage>
我有一个 xaml 文件,我在其中放置了选择器和一个用于填充它的代码。下面使用的代码:
Dictionary<string, int> list = new Dictionary<string, int>
{
{ "Aqua", 6},
{ "Red", 1 },
{ "Silver", 2 },
{ "Teal", 3 },
{ "White", 4 },
{ "Yellow", 55 }
};
foreach (string item in list.Keys)
{
ddlPurpose.Items.Add(item);
}
我试图在 select 黄色时获取值 55,但我唯一得到的是 5。我正在使用它来获取 selected 值
var val1 = ddlPurpose.SelectedItem.ToString();
var val2 = ddlPurpose.SelectedIndex;
难道连键值都可以拿到?已经查看了 BindablePicker,但这似乎根本不起作用。非常感谢对此的任何帮助。
我猜你的意思是:
var pSelectedIndex = ddlPurpose.SelectedIndex;
var selectedKey = list.Values.ElementAt(pSelectedIndex);
我建议您熟悉 MVVM,在这种特定情况下,我建议您熟悉 Behaviors。我写了一个小例子来演示使用 MVVM 的样子:
public class PickerKeyValueTestViewModel : INotifyPropertyChanged
{
static Dictionary<string, int> colors { get; } = new Dictionary<string, int>
{
{ "Aqua", 6 },
{ "Red", 1 },
{ "Silver", 2 },
{ "Teal", 3 },
{ "White", 4 },
{ "Yellow", 55 }
};
public List<string> Colors { get; } = colors.Keys.ToList();
public string SelectedColor { get; set; }
public void OnSelectedColorChanged()
{
if (string.IsNullOrEmpty(SelectedColor)) return;
var selectedValue = colors[SelectedColor];
}
// Using PropertyChanged.Fody
public event PropertyChangedEventHandler PropertyChanged;
}
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PickerKeyValueTest"
x:Class="PickerKeyValueTest.PickerKeyValueTestPage">
<ContentPage.BindingContext>
<local:PickerKeyValueTestViewModel />
</ContentPage.BindingContext>
<StackLayout
Margin="25">
<Picker
ItemsSource="{Binding Colors}"
SelectedItem="{Binding SelectedColor}">
</Picker>
</StackLayout>
</ContentPage>