|
@@ -1,10 +1,6 @@
|
|
|
using System;
|
|
|
-using System.Reactive;
|
|
|
-using System.Reactive.Disposables;
|
|
|
-using System.Reactive.Linq;
|
|
|
-using System.Reactive.Subjects;
|
|
|
-using Avalonia.Data;
|
|
|
using Avalonia.Reactive;
|
|
|
+using Avalonia.Data;
|
|
|
|
|
|
namespace Avalonia
|
|
|
{
|
|
@@ -127,108 +123,6 @@ namespace Avalonia
|
|
|
property ?? throw new ArgumentNullException(nameof(property)));
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Gets a subject for an <see cref="AvaloniaProperty"/>.
|
|
|
- /// </summary>
|
|
|
- /// <param name="o">The object.</param>
|
|
|
- /// <param name="property">The property.</param>
|
|
|
- /// <param name="priority">
|
|
|
- /// The priority with which binding values are written to the object.
|
|
|
- /// </param>
|
|
|
- /// <returns>
|
|
|
- /// An <see cref="ISubject{Object}"/> which can be used for two-way binding to/from the
|
|
|
- /// property.
|
|
|
- /// </returns>
|
|
|
- public static ISubject<object?> GetSubject(
|
|
|
- this AvaloniaObject o,
|
|
|
- AvaloniaProperty property,
|
|
|
- BindingPriority priority = BindingPriority.LocalValue)
|
|
|
- {
|
|
|
- return Subject.Create<object?>(
|
|
|
- Observer.Create<object?>(x => o.SetValue(property, x, priority)),
|
|
|
- o.GetObservable(property));
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Gets a subject for an <see cref="AvaloniaProperty"/>.
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">The property type.</typeparam>
|
|
|
- /// <param name="o">The object.</param>
|
|
|
- /// <param name="property">The property.</param>
|
|
|
- /// <param name="priority">
|
|
|
- /// The priority with which binding values are written to the object.
|
|
|
- /// </param>
|
|
|
- /// <returns>
|
|
|
- /// An <see cref="ISubject{T}"/> which can be used for two-way binding to/from the
|
|
|
- /// property.
|
|
|
- /// </returns>
|
|
|
- public static ISubject<T> GetSubject<T>(
|
|
|
- this AvaloniaObject o,
|
|
|
- AvaloniaProperty<T> property,
|
|
|
- BindingPriority priority = BindingPriority.LocalValue)
|
|
|
- {
|
|
|
- return Subject.Create<T>(
|
|
|
- Observer.Create<T>(x => o.SetValue(property, x, priority)),
|
|
|
- o.GetObservable(property));
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Gets a subject for a <see cref="AvaloniaProperty"/>.
|
|
|
- /// </summary>
|
|
|
- /// <param name="o">The object.</param>
|
|
|
- /// <param name="property">The property.</param>
|
|
|
- /// <param name="priority">
|
|
|
- /// The priority with which binding values are written to the object.
|
|
|
- /// </param>
|
|
|
- /// <returns>
|
|
|
- /// An <see cref="ISubject{Object}"/> which can be used for two-way binding to/from the
|
|
|
- /// property.
|
|
|
- /// </returns>
|
|
|
- public static ISubject<BindingValue<object?>> GetBindingSubject(
|
|
|
- this AvaloniaObject o,
|
|
|
- AvaloniaProperty property,
|
|
|
- BindingPriority priority = BindingPriority.LocalValue)
|
|
|
- {
|
|
|
- return Subject.Create<BindingValue<object?>>(
|
|
|
- Observer.Create<BindingValue<object?>>(x =>
|
|
|
- {
|
|
|
- if (x.HasValue)
|
|
|
- {
|
|
|
- o.SetValue(property, x.Value, priority);
|
|
|
- }
|
|
|
- }),
|
|
|
- o.GetBindingObservable(property));
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Gets a subject for a <see cref="AvaloniaProperty"/>.
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">The property type.</typeparam>
|
|
|
- /// <param name="o">The object.</param>
|
|
|
- /// <param name="property">The property.</param>
|
|
|
- /// <param name="priority">
|
|
|
- /// The priority with which binding values are written to the object.
|
|
|
- /// </param>
|
|
|
- /// <returns>
|
|
|
- /// An <see cref="ISubject{T}"/> which can be used for two-way binding to/from the
|
|
|
- /// property.
|
|
|
- /// </returns>
|
|
|
- public static ISubject<BindingValue<T>> GetBindingSubject<T>(
|
|
|
- this AvaloniaObject o,
|
|
|
- AvaloniaProperty<T> property,
|
|
|
- BindingPriority priority = BindingPriority.LocalValue)
|
|
|
- {
|
|
|
- return Subject.Create<BindingValue<T>>(
|
|
|
- Observer.Create<BindingValue<T>>(x =>
|
|
|
- {
|
|
|
- if (x.HasValue)
|
|
|
- {
|
|
|
- o.SetValue(property, x.Value, priority);
|
|
|
- }
|
|
|
- }),
|
|
|
- o.GetBindingObservable(property));
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Binds an <see cref="AvaloniaProperty"/> to an observable.
|
|
|
/// </summary>
|
|
@@ -407,13 +301,7 @@ namespace Avalonia
|
|
|
Action<TTarget, AvaloniaPropertyChangedEventArgs> action)
|
|
|
where TTarget : AvaloniaObject
|
|
|
{
|
|
|
- return observable.Subscribe(e =>
|
|
|
- {
|
|
|
- if (e.Sender is TTarget target)
|
|
|
- {
|
|
|
- action(target, e);
|
|
|
- }
|
|
|
- });
|
|
|
+ return observable.Subscribe(new ClassHandlerObserver<TTarget>(action));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -431,13 +319,7 @@ namespace Avalonia
|
|
|
this IObservable<AvaloniaPropertyChangedEventArgs<TValue>> observable,
|
|
|
Action<TTarget, AvaloniaPropertyChangedEventArgs<TValue>> action) where TTarget : AvaloniaObject
|
|
|
{
|
|
|
- return observable.Subscribe(e =>
|
|
|
- {
|
|
|
- if (e.Sender is TTarget target)
|
|
|
- {
|
|
|
- action(target, e);
|
|
|
- }
|
|
|
- });
|
|
|
+ return observable.Subscribe(new ClassHandlerObserver<TTarget, TValue>(action));
|
|
|
}
|
|
|
|
|
|
private class BindingAdaptor : IBinding
|
|
@@ -458,5 +340,57 @@ namespace Avalonia
|
|
|
return InstancedBinding.OneWay(_source);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private class ClassHandlerObserver<TTarget, TValue> : IObserver<AvaloniaPropertyChangedEventArgs<TValue>>
|
|
|
+ {
|
|
|
+ private readonly Action<TTarget, AvaloniaPropertyChangedEventArgs<TValue>> _action;
|
|
|
+
|
|
|
+ public ClassHandlerObserver(Action<TTarget, AvaloniaPropertyChangedEventArgs<TValue>> action)
|
|
|
+ {
|
|
|
+ _action = action;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnCompleted()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnError(Exception error)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnNext(AvaloniaPropertyChangedEventArgs<TValue> value)
|
|
|
+ {
|
|
|
+ if (value.Sender is TTarget target)
|
|
|
+ {
|
|
|
+ _action(target, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class ClassHandlerObserver<TTarget> : IObserver<AvaloniaPropertyChangedEventArgs>
|
|
|
+ {
|
|
|
+ private readonly Action<TTarget, AvaloniaPropertyChangedEventArgs> _action;
|
|
|
+
|
|
|
+ public ClassHandlerObserver(Action<TTarget, AvaloniaPropertyChangedEventArgs> action)
|
|
|
+ {
|
|
|
+ _action = action;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnCompleted()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnError(Exception error)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnNext(AvaloniaPropertyChangedEventArgs value)
|
|
|
+ {
|
|
|
+ if (value.Sender is TTarget target)
|
|
|
+ {
|
|
|
+ _action(target, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|