2017-11-17 11 views
3

結果がグループ化されている検索機能を実装するXamarinアプリケーションがあります。したがって、私はグループ化されたリストビューを使用しました。ObservableCollectionをクリアすると例外が発生する

private async void SearchRecipient() 
{ 
    IList<Recipient> recipients = null; 

    if (!string.IsNullOrWhiteSpace(RecipientSearched)) 
    { 
     recipients = await _service.GetRecipients(RecipientSearched.ToLower()); 

     FilteredRecipients.Clear(); 
     _userGroupedList.Clear(); 
     _officeGroupedList.Clear(); 

     if (recipients != null) 
     { 
      foreach (var r in recipients) 
      { 
       // Some logic to populate collections 
       _userGroupedList.Add(selectable); 
       _officeGroupedList.Add(selectable); 
      } 

      if (_userGroupedList.Count > 0) 
       FilteredRecipients.Add(_userGroupedList); 
      if (_officeGroupedList.Count > 0) 
       FilteredRecipients.Add(_officeGroupedList); 
     } 
    } 
} 
FilteredRecipients

_userGroupedList_officeGroupedListListあるが、ObservableCollectionあります。

public SearchRecipientPageModel() 
{ 
    FilteredRecipients = new ObservableCollection<GroupedRecipientModel>(); 
    _userGroupedList = new GroupedRecipientModel("User"); 
    _officeGroupedList = new GroupedRecipientModel("Office"); 
} 

検索とグループ化も同様です。私は二度目の検索を繰り返すときに問題が起こるとFilteredRecipients.Clear()は、次の例外スロー:

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' 

UPDATE 問題の結果のいくつかの項目をチェックボックスで選択されている場合にのみ起こるようです。 CheckboxをSwitchで置き換えたので、Checkbox Rendererの実装が原因であると思います。 TwoWay Mode Bindingで動作させるためにいくつか問題がありましたが、正しく修正しなかった可能性があります。

public class CustomCheckBox : View 
{ 
    public bool Checked 
    { 
     get => (bool)GetValue(CheckedProperty); 
     set => SetValue(CheckedProperty, value); 
    } 

    public ICommand Command 
    { 
     get => (ICommand)GetValue(CommandProperty); 
     set => SetValue(CommandProperty, value); 
    } 

    public object CommandParameter 
    { 
     get => GetValue(CommandParameterProperty); 
     set => SetValue(CommandParameterProperty, value); 
    } 

    public static readonly BindableProperty CommandParameterProperty = 
           BindableProperty.Create("CommandParameter", typeof(object), typeof(CustomCheckBox), default(object)); 

    public static readonly BindableProperty CheckedProperty = 
           BindableProperty.Create("Checked", typeof(bool), typeof(CustomCheckBox), default(bool), propertyChanged: OnChecked); 

    public static readonly BindableProperty CommandProperty = 
       BindableProperty.Create("Command", typeof(ICommand), typeof(CustomCheckBox), default(ICommand)); 


    private static void OnChecked(BindableObject bindable, object oldValue, object newValue) 
    { 
     if (bindable is CustomCheckBox checkbox) 
     { 
      object parameter = checkbox.CommandParameter ?? newValue; 

      if (checkbox.Command != null && checkbox.Command.CanExecute(parameter)) 
       checkbox.Command.Execute(parameter); 
     } 
    } 
} 

レンダラチェックイベントが二回するたびに発生しますので、

public class CustomCheckBoxRenderer : ViewRenderer<CustomCheckBox, CheckBox> 
{ 
    protected override void OnElementChanged(ElementChangedEventArgs<CustomCheckBox> e) 
    { 
     base.OnElementChanged(e); 
     if (Control == null && Element != null) 
      SetNativeControl(new CheckBox()); 

     if (Control != null) 
     {     
      Control.IsChecked = Element.Checked; 
      Control.Checked += (s, r) => { Element.Checked = true; }; 
      Control.Unchecked += (s, r) => { Element.Checked = false; }; 
     } 
    } 

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     base.OnElementPropertyChanged(sender, e); 

     if (e.PropertyName == nameof(Element.Checked)) 
      Control.IsChecked = Element.Checked; 
    } 
} 

はまた、私はまだこのレンダラーのバグを調査しています。

+2

もっとコードを投稿できますか?いくつかの "SelectedItem"プロパティがありますか?おそらく "クリア"の問題ではなく、 "クリア"に接続された何か... –

+0

私のXAMLはとても簡単です。私はちょうど 'ItemsSource'と' IsGroupingEnabled'プロパティを設定しました... – user2297037

+0

最初の投稿にいくつかの詳細を追加しました。 – user2297037

答えて

0

コードの下トライ代わりにクリア()

FilteredRecipients = new ObservableCollection<YourModel>(); 

編集

チェックイベントのためのソリューションを二回するたびに発生します。

if (e.OldElement != null) 
{ 
     Control.Checked -= (s, r) => { Element.Checked = true; }; 
     Control.Unchecked -= (s, r) => { Element.Checked = false; }; 
} 

if (e.NewElement != null) 
{ 
     Control.Checked += (s, r) => { Element.Checked = true; }; 
     Control.Unchecked += (s, r) => { Element.Checked = false; }; 
} 
+0

Not working ...コンストラクタを呼び出すには、NotifyPropertyChangedを実装する必要があるので、追加しました。しかし、この例外はまだXAMLコードによってスローされますが、この例外はスローされています... – user2297037

+0

FilteredRecipients宣言と初期化コードをヘルプに役立てます。 –

+0

最初の投稿を更新しました – user2297037

関連する問題