This commit is contained in:
neuecc 2021-09-03 18:26:37 +09:00
parent 501a6c3c0d
commit 7dd822fc9b
2 changed files with 100 additions and 16 deletions

View File

@ -54,10 +54,30 @@ PM> Install-Package [ObservableCollections](https://www.nuget.org/packages/Obser
create new `ObservableList<T>`, `ObservableDictionary<TKey, TValue>`, `ObservableHashSet<T>`, `ObservableQueue<T>`, `ObservableStack<T>`, `ObservableRingBuffer<T>`, `ObservableFixedSizeRingBuffer<T>`. create new `ObservableList<T>`, `ObservableDictionary<TKey, TValue>`, `ObservableHashSet<T>`, `ObservableQueue<T>`, `ObservableStack<T>`, `ObservableRingBuffer<T>`, `ObservableFixedSizeRingBuffer<T>`.
```csharp
// Blazor simple sample.
public partial class Index : IDisposable
{
ObservableList<int> list;
public ISynchronizedView<int, int> ItemsView { get; set; }
protected override void OnInitialized()
{
list = new ObservableList<int>();
ItemsView = list.CreateView(x => x);
ItemsView.CollectionStateChanged += action =>
{
InvokeAsync(StateHasChanged);
};
}
public void Dispose()
{
ItemsView.Dispose();
}
}
```
```csharp ```csharp
// WPF simple sample. // WPF simple sample.
@ -71,7 +91,6 @@ public MainWindow()
this.DataContext = this; this.DataContext = this;
list = new ObservableList<int>(); list = new ObservableList<int>();
list.AddRange(new[] { 1, 10, 188 });
ItemsView = list.CreateSortedView(x => x, x => x, comparer: Comparer<int>.Default).WithINotifyCollectionChanged(); ItemsView = list.CreateSortedView(x => x, x => x, comparer: Comparer<int>.Default).WithINotifyCollectionChanged();
BindingOperations.EnableCollectionSynchronization(ItemsView, new object()); // for ui synchronization safety of viewmodel BindingOperations.EnableCollectionSynchronization(ItemsView, new object()); // for ui synchronization safety of viewmodel
@ -83,16 +102,83 @@ protected override void OnClosed(EventArgs e)
} }
``` ```
```csharp
// Unity, with filter sample.
public class SampleScript : MonoBehaviour
{
public Button prefab;
public GameObject root;
ObservableRingBuffer<int> collection;
ISynchronizedView<GameObject> view;
void Start()
{
collection = new ObservableRingBuffer<int>();
view = collection.CreateView(x =>
{
var item = GameObject.Instantiate(prefab);
item.GetComponentInChildren<Text>().text = x.ToString();
return item.gameObject;
});
view.AttachFilter(new GameObjectFilter(root));
}
void OnDestroy()
{
view.Dispose();
}
public class GameObjectFilter : ISynchronizedViewFilter<int, GameObject>
{
readonly GameObject root;
public GameObjectFilter(GameObject root)
{
this.root = root;
}
public void OnCollectionChanged(ChangedKind changedKind, int value, GameObject view)
{
if (changedKind == ChangedKind.Add)
{
view.transform.SetParent(root.transform);
}
else if (changedKind == ChangedKind.Remove)
{
GameObject.Destroy(view);
}
}
public bool IsMatch(int value, GameObject view)
{
return value % 2 == 0;
}
public void WhenTrue(int value, GameObject view)
{
view.SetActive(true);
}
public void WhenFalse(int value, GameObject view)
{
view.SetActive(false);
}
}
}
```
TODO: write more usage... TODO: write more usage...
View/SoretedView View/SoretedView
--- ---
Filter
---
Collections Collections
--- ---
Unity Unity
--- ---

View File

@ -3,29 +3,29 @@ using ObservableCollections;
namespace BlazorApp.Pages; namespace BlazorApp.Pages;
public partial class Index public partial class Index : IDisposable
{ {
ObservableList<int> list; ObservableList<int> list;
public ISynchronizedView<int, int> ItemsView { get; set; } public ISynchronizedView<int, int> ItemsView { get; set; }
int adder = 99; int adder = 99;
RenderFragment fragment;
protected override void OnInitialized() protected override void OnInitialized()
{ {
list = new ObservableList<int>(); list = new ObservableList<int>();
list.AddRange(new[] { 1, 10, 188 }); ItemsView = list.CreateView(x => x);
ItemsView = list.CreateSortedView(x => x, x => x, comparer: Comparer<int>.Default).WithINotifyCollectionChanged();
ItemsView.CollectionStateChanged += action =>
fragment = builder =>
{ {
builder.GetFrames(); InvokeAsync(StateHasChanged);
}; };
} }
public void Dispose()
{
ItemsView.Dispose();
}
void OnClick() void OnClick()
@ -33,8 +33,6 @@ public partial class Index
ThreadPool.QueueUserWorkItem(_ => ThreadPool.QueueUserWorkItem(_ =>
{ {
list.Add(adder++); list.Add(adder++);
_ = InvokeAsync(StateHasChanged);
}); });
} }
} }