我使用Visual Studio 2017创建了一个跨平台的Xamarin.Forms应用程序,该应用程序包含一个带有两个ListView的TabPage容器。它工作得很好,直到我尝试添加Picker控件。当我尝试使用添加的选择器进行构建时,我收到以下错误:
“序列不包含元素”
这是我正在添加的Xaml:
<Picker x:Name="thePicker">
<Picker.Items>
<x:String>Make Call</x:String>
<x:String>Send Text</x:String>
</Picker.Items>
</Picker>
感谢您提供的任何帮助。
答案 0 :(得分:0)
我认为您的Picker
不属于任何类型的Layout
。
同样使用新的Picker可绑定API,您的项目列表将变为:
<ContentPage.Content>
<StackLayout>
<Label Text="StackOverflow"/>
<Picker x:Name="thePicker">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Make Call</x:String>
<x:String>Send Text</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
</StackLayout>
</ContentPage.Content>
答案 1 :(得分:0)
通过您的输入,我找到了一个可行的解决方案。使用的XAML:
<ContentPage.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0" Padding="20">
<Picker x:Name="actionPicker" IsEnabled="False" IsVisible="False" SelectedIndexChanged="cellPhoneSelected" Unfocused="actionPicker_Unfocused" HeightRequest="200">
<Picker.Items>
<x:String>Make Call</x:String>
<x:String>Send Text</x:String>
</Picker.Items>
</Picker>
</StackLayout>
<StackLayout Grid.Row="0" Grid.Column="0">
<ListView x:Name="listView" ItemSelected="OnItemSelected" HasUnevenRows="True" ItemTemplate="{StaticResource employeeDataTemplateSelector}" IsGroupingEnabled="True" IsPullToRefreshEnabled="True" Refreshing="employee_Refreshing">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell Height="40">
<Grid Padding="4" BackgroundColor="LightGray" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Text="{Binding LongName}" Margin="0,8,0,0" FontAttributes="Bold" Grid.Column="0" Grid.Row="0" VerticalOptions="FillAndExpand" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
</ListView>
</StackLayout>
</Grid>
</ContentPage.Content>
使用的代码:
void cellPhoneSelected(object sender, EventArgs e)
{
var picker = (Picker)sender;
int selectedIndex = picker.SelectedIndex;
switch (selectedIndex)
{
case 0:
Device.OpenUri(new Uri(String.Format("tel:{0}", phone)));
break;
case 1:
Device.OpenUri(new Uri(String.Format("sms:{0}", phone)));
break;
}
actionPicker.IsEnabled = false;
actionPicker.IsVisible = false;
}
void phoneSelected(object sender, EventArgs args) {
phone = ((Label)sender).Text;
switch (phone.Substring(0, 1))
{
case "W":
phone = phone.Replace("W: ", "").Replace("-", "").Replace("(", "").Replace(")", "").Replace(".", "").Replace(" ", "");
if (phone.Length == 7)
{
phone = "405" + phone;
}
Device.OpenUri(new Uri(String.Format("tel:{0}", phone)));
break;
case "C":
phone = phone.Replace("C: ", "").Replace("-", "").Replace("(", "").Replace(")", "").Replace(".", "").Replace(" ", "");
if (phone.Length == 7)
{
phone = "405" + phone;
}
actionPicker.SelectedIndex = -1;
actionPicker.IsEnabled = true;
actionPicker.IsVisible = true;
actionPicker.Focus();
break;
case "H":
phone = phone.Replace("H: ", "").Replace("-", "").Replace("(", "").Replace(")", "").Replace(".", "").Replace(" ", "");
if (phone.Length == 7)
{
phone = "405" + phone;
}
Device.OpenUri(new Uri(String.Format("tel:{0}", phone)));
break;
case "P":
for (var i = 0; i < employees.Count; i++)
{
for (var c = 0; c < employees[i].Count; c++)
{
if (employees[i][c].Pager == phone)
{
if (employees[i][c].Pager2 != null)
{
phone = employees[i][c].Pager2;
Device.OpenUri(new Uri(String.Format("mailto:{0}", phone)));
}
break;
}
else
{
if (employees[i][c].CellPhone == phone)
{
if (employees[i][c].Pager2 != null)
{
phone = employees[i][c].Pager2;
Device.OpenUri(new Uri(String.Format("mailto:{0}", phone)));
}
break;
}
}
}
}
break;
}
}
private void actionPicker_Unfocused(object sender, FocusEventArgs e)
{
actionPicker.IsEnabled = false;
actionPicker.IsVisible = false;
}
“Unfocused”方法用于在选择器上单击“取消”按钮时隐藏选择器。
再次感谢您的协助!