2017-05-10 4 views
0

2つのリストビューを持つTabPageコンテナを含むVisual Studio 2017でクロスプラットフォームのXamarin.Formsアプリケーションを作成しました。ピッカーコントロールを追加しようとするまでうまくいきました。任意の助けXamarin.Formsピッカーを追加すると「シーケンスに要素がありません」エラーが発生する

<Picker x:Name="thePicker"> 
     <Picker.Items> 
      <x:String>Make Call</x:String> 
      <x:String>Send Text</x:String> 
     </Picker.Items> 
    </Picker> 

感謝を:

これは、XAML私が追加しているである「シーケンスに要素が含まれていない」:ピッカーを追加して、私が構築しようとした際、私は次のエラーを取得しますあなたは提供することができます。

答えて

0

あなたのPickerは、Layoutのいずれの種類でもないと思います。

はまた新しいピッカーバインド可能な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> 
+0

すばやく返信いただきありがとうございます。 レイアウト内にリストビューとピッカーがないことが問題の原因でした。私の問題は今、リストビュー項目をラベルをクリックすると、リストビューを画面いっぱいにして、リストビュー上にピッカーポップアップを表示しようとしています。 – user680891

+0

@ user680891 xaml/codeで別の質問を投稿することをお勧めします。 – SushiHangover

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; 
    } 

は「ピントの合っていない」方法は、彼らがピッカーで[キャンセル]ボタンをクリックしたときにピッカーを隠すために使用されます。

もう一度お手数をおかけします。

+0

また、XAMLについてのメモを含めることも意味しました。リストビューXAMLの後にピッカーXAMLを配置すると、リストビューにアクセスできなくなります。どうやら、レイアウトの最後のコントロールは一番上のコントロールです。 – user680891

関連する問題