100 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Specialized;
2024-02-15 17:13:16 +09:00
using R3;
using System.Linq;
using ObservableCollections;
2024-09-03 18:54:59 +09:00
using System.Collections;
using System.Collections.Generic;
2024-10-03 19:29:51 +09:00
using System.Threading.Tasks.Sources;
using System.Reflection.Emit;
2024-02-15 17:13:16 +09:00
var list = new ObservableList<Person>()
2024-10-03 19:29:51 +09:00
{
new (){ Age = 10, Name = "John" },
new (){ Age = 22, Name = "Jeyne" },
new (){ Age = 30, Name = "Mike" },
};
var view = list.CreateWritableView(x => x.Name);
view.AttachFilter(x => x.Age >= 20);
var bindable = view.ToWritableNotifyCollectionChanged((string? newView, Person original, ref bool setValue) =>
{
if (setValue)
{
// default setValue == true is Set operation
original.Name = newView;
2024-02-15 17:13:16 +09:00
// You can modify setValue to false, it does not set original collection to new value.
// For mutable reference types, when there is only a single,
// bound View and to avoid recreating the View, setting false is effective.
// Otherwise, keeping it true will set the value in the original collection as well,
// and change notifications will be sent to lower-level Views(the delegate for View generation will also be called anew).
setValue = false;
return original;
}
else
{
// default setValue == false is Add operation
return new Person { Age = null, Name = newView };
}
});
2024-10-03 10:19:16 +09:00
// bindable[0] = "takoyaki";
2021-08-04 09:30:34 +09:00
2024-10-03 19:29:51 +09:00
foreach (var item in view)
{
Console.WriteLine(item);
}
Console.WriteLine("---");
foreach (var item in list)
{
Console.WriteLine((item.Age, item.Name));
}
public class Person
{
public int? Age { get; set; }
public string? Name { get; set; }
}
//var buffer = new ObservableFixedSizeRingBuffer<int>(5);
//var view = buffer.CreateView(value => value);
//view.AttachFilter(value => value % 2 == 1); // when filtered, mismatch...!
////{
//// INotifyCollectionChangedSynchronizedViewList created from ISynchronizedView with a filter.
//var collection = view.ToNotifyCollectionChanged();
//// Not disposed here.
////}
//buffer.AddFirst(1);
//buffer.AddFirst(1);
//buffer.AddFirst(2);
//buffer.AddFirst(3);
//buffer.AddFirst(5);
//buffer.AddFirst(8); // Argument out of range
//buffer.AddFirst(13);
//foreach (var item in collection)
//{
// Console.WriteLine(item);
//}
//Console.WriteLine("---");
//foreach (var item in view)
//{
// Console.WriteLine(item);
//}
//Console.WriteLine("---");
//foreach (var item in buffer)
//{
// Console.WriteLine(item);
//}