我正在尝试获取一个(国家/地区)列表来填充“Picker”#39;领域。现在当我运行应用程序并点击列表选择器字段时,我所看到的就是:
代码(.cs):
List<string> countries = new List<string>
{
....
"Western Sahara",
"Yemen",
"Yugoslavia",
"Zambia",
"Zimbabwe"
};
List<string> Countries => countries;
string SelectedCountry;
int countriesSelectedIndex;
public int CountriesSelectedIndex
{
get
{
return countriesSelectedIndex;
}
set
{
if (countriesSelectedIndex != value)
{
countriesSelectedIndex = value;
// trigger some action to take such as updating other labels or fields
OnPropertyChanged(nameof(CountriesSelectedIndex));
Country = Countries[countriesSelectedIndex];
}
}
}
public string Country
{
get
{
return _country;
}
set
{
_country = value;
OnPropertyChanged(nameof(Country));
}
}
代码(.xaml):
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ViewModels="clr-namespace:LoginSystem.ViewModels"
BackgroundColor="Azure"
x:Class="LoginSystem.Views.UserProfileINC">
<ContentPage.Content>
<StackLayout Padding="10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<!--Gender Country Address Province Postal-->
</Grid.ColumnDefinitions>
<Label Text="Gender" Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
<Entry Text="{Binding Gender}" Grid.Row="0" Grid.Column="1" />
<Label Text="Country" Grid.Row="1" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
<Picker x:Name="picker" Grid.Row="1" Grid.Column="1" SelectedIndex="{Binding CountriesSelectedIndex}" ItemsSource="{Binding Countries}"/>
<!--<Entry Text="{Binding Country}" Grid.Row="1" Grid.Column="1" />-->
<Label Text="Address" Grid.Row="2" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
<Entry Text="{Binding Address}" Grid.Row="2" Grid.Column="1" />
<Label Text="Province" Grid.Row="3" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
<Entry Text="{Binding Province}" Grid.Row="3" Grid.Column="1" />
<Label Text="Postal" Grid.Row="4" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
<Entry Text="{Binding Postal}" Grid.Row="4" Grid.Column="1" />
</Grid>
<Button Text="Done!" HorizontalOptions="FillAndExpand" BackgroundColor="Blue" TextColor="White" Command="{Binding Button_Clicked}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
这是绑定选择器的行:
<Picker x:Name="picker" Grid.Row="1" Grid.Column="1" SelectedIndex="{Binding CountriesSelectedIndex}" ItemsSource="{Binding Countries}"/>