diff --git a/README.md b/README.md index 6669244..94ff256 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Observable> IObservableCollection.ObserveReset() Observable> IObservableCollection.ObserveReset() Observable IObservableCollection.ObserveClear() Observable<(int Index, int Count)> IObservableCollection.ObserveReverse() -Observable<(int Index, int Count, IComparer Comparer)> IObservableCollection.ObserveSort() +Observable<(int Index, int Count, IComparer? Comparer)> IObservableCollection.ObserveSort() Observable IObservableCollection.ObserveCountChanged() ``` @@ -355,6 +355,10 @@ public class WpfDispatcherCollection(Dispatcher dispatcher) : ICollectionEventDi } ``` +`ToNotifyCollectionChanged()` can also be called without going through a View. In this case, it's guaranteed that no filters will be applied, making it faster. If you want to apply filters, please generate a View before calling it. Additionally, `ObservableList` has a variation called `ToNotifyCollectionChangedSlim()`. This option doesn't generate a list for the View and shares the actual data, making it the fastest and most memory-efficient option. However, range operations such as `AddRange`, `InsertRange` and `RemoveRange` are not supported by WPF (or Avalonia), so they will throw runtime exceptions. + +Views and ToNotifyCollectionChanged are internally connected by events, so they need to be `Dispose` to release those connections. + Unity --- In Unity projects, you can installing `ObservableCollections` with [NugetForUnity](https://github.com/GlitchEnzo/NuGetForUnity). If R3 integration is required, similarly install `ObservableCollections.R3` via NuGetForUnity. @@ -376,21 +380,25 @@ public class SampleScript : MonoBehaviour { var item = GameObject.Instantiate(prefab); item.GetComponentInChildren().text = x.ToString(); + + // add to root + item.transform.SetParent(root.transform); + return item.gameObject; }); view.ViewChanged += View_ViewChanged; } void View_ViewChanged(in SynchronizedViewChangedEventArgs eventArgs) - { - if (eventArgs.Action == NotifyCollectionChangedAction.Add) - { - eventArgs.NewItem.View.transform.SetParent(root.transform); - } - else if (NotifyCollectionChangedAction.Remove) + { + // hook remove event + if (NotifyCollectionChangedAction.Remove) { GameObject.Destroy(eventArgs.OldItem.View); } + + // hook for Filter attached, clear, etc... + // if (NotifyCollectionChangedAction.Reset) { } } void OnDestroy() diff --git a/sandbox/AvaloniaApp/MainWindow.axaml b/sandbox/AvaloniaApp/MainWindow.axaml index cfd3b92..9530d9b 100644 --- a/sandbox/AvaloniaApp/MainWindow.axaml +++ b/sandbox/AvaloniaApp/MainWindow.axaml @@ -7,7 +7,14 @@ Title="AvaloniaApp"> -