ObservableCollections/sandbox/WpfApp/MainWindow.xaml.cs

120 lines
3.3 KiB
C#
Raw Normal View History

2021-08-13 18:35:45 +09:00
using ObservableCollections;
2024-02-22 18:39:06 +09:00
using R3;
2021-08-13 18:35:45 +09:00
using System;
2021-08-04 09:30:34 +09:00
using System.Collections.Generic;
using System.Collections.Specialized;
2024-02-22 18:39:06 +09:00
using System.Diagnostics;
2021-08-04 09:30:34 +09:00
using System.Linq;
using System.Text;
2021-08-13 18:35:45 +09:00
using System.Threading;
2021-08-04 09:30:34 +09:00
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
2021-08-04 09:30:34 +09:00
namespace WpfApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
2024-02-22 18:39:06 +09:00
//ObservableList<int> list;
//public INotifyCollectionChangedSynchronizedView<int> ItemsView { get; set; }
2021-08-13 18:35:45 +09:00
2021-08-04 09:30:34 +09:00
public MainWindow()
{
InitializeComponent();
2021-08-13 18:35:45 +09:00
2021-09-08 19:20:32 +09:00
2024-02-22 18:39:06 +09:00
R3.WpfProviderInitializer.SetDefaultObservableSystem(x =>
{
Trace.WriteLine(x);
});
2021-08-13 18:35:45 +09:00
2024-02-22 18:39:06 +09:00
this.DataContext = new ViewModel();
2021-08-13 18:35:45 +09:00
// Dispatcher.BeginInvoke(
2021-08-13 18:35:45 +09:00
2024-02-22 18:39:06 +09:00
//list = new ObservableList<int>();
//list.AddRange(new[] { 1, 10, 188 });
//ItemsView = list.CreateSortedView(x => x, x => x, comparer: Comparer<int>.Default).ToNotifyCollectionChanged();
//BindingOperations.EnableCollectionSynchronization(ItemsView, new object());
2021-08-13 18:35:45 +09:00
}
2024-02-22 18:39:06 +09:00
//int adder = 99;
2021-08-13 18:35:45 +09:00
2024-02-22 18:39:06 +09:00
//private void Button_Click(object sender, RoutedEventArgs e)
//{
// ThreadPool.QueueUserWorkItem(_ =>
// {
// list.Add(adder++);
// });
//}
//protected override void OnClosed(EventArgs e)
//{
// ItemsView.Dispose();
//}
}
public class ViewModel
{
private ObservableList<int> observableList { get; } = new ObservableList<int>();
public INotifyCollectionChangedSynchronizedView<int> ItemsView { get; }
public ReactiveCommand<Unit> AddCommand { get; } = new ReactiveCommand<Unit>();
2024-02-22 18:39:06 +09:00
public ReactiveCommand<Unit> ClearCommand { get; } = new ReactiveCommand<Unit>();
public ViewModel()
2021-08-13 18:35:45 +09:00
{
2024-02-22 18:39:06 +09:00
observableList.Add(1);
observableList.Add(2);
ItemsView = observableList.CreateView(x => x).ToNotifyCollectionChanged(SynchronizationContextCollectionEventDispatcher.Current);
2024-02-22 18:39:06 +09:00
// ItemsView = observableList.CreateView(x => x).ToNotifyCollectionChanged();
// BindingOperations.EnableCollectionSynchronization(ItemsView, new object());
AddCommand.Subscribe(_ =>
{
ThreadPool.QueueUserWorkItem(_ =>
{
observableList.Add(Random.Shared.Next());
});
});
2024-02-22 18:39:06 +09:00
// var iii = 10;
ClearCommand.Subscribe(_ =>
2021-08-13 18:35:45 +09:00
{
2024-02-22 18:39:06 +09:00
// observableList.Add(iii++);
observableList.Clear();
2021-08-13 18:35:45 +09:00
});
2021-08-04 09:30:34 +09:00
}
}
public class WpfDispatcherCollection(Dispatcher dispatcher) : ICollectionEventDispatcher
{
public void Post(CollectionEventDispatcherEventArgs ev)
{
dispatcher.InvokeAsync(() =>
{
ev.Invoke();
});
}
}
2024-02-22 18:39:06 +09:00
}