csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
2.2 KiB
53 lines
2.2 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="PerspexObjectExtensions.cs" company="Steven Kirk">
|
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace Perspex
|
|
{
|
|
using System;
|
|
using System.Reactive.Linq;
|
|
|
|
/// <summary>
|
|
/// Provides extension methods for <see cref="PerspexObject"/> and related classes.
|
|
/// </summary>
|
|
public static class PerspexObjectExtensions
|
|
{
|
|
/// <summary>
|
|
/// Subscribes to a property changed notifications for changes that originate from a
|
|
/// <typeparamref name="TTarget"/>.
|
|
/// </summary>
|
|
/// <typeparam name="TTarget">The type of the property change sender.</typeparam>
|
|
/// <param name="observable">The property changed observable.</param>
|
|
/// <param name="handler">Given a TTarget, returns the handler.</param>
|
|
/// <returns>A disposable that can be used to terminate the subscription.</returns>
|
|
public static IDisposable AddClassHandler<TTarget>(
|
|
this IObservable<PerspexPropertyChangedEventArgs> observable,
|
|
Func<TTarget, Action<PerspexPropertyChangedEventArgs>> handler)
|
|
where TTarget : class
|
|
{
|
|
return observable.Subscribe(e => SubscribeAdapter(e, handler));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Observer method for <see cref="AddClassHandler{TTarget}(IObservable{PerspexPropertyChangedEventArgs},
|
|
/// Func{TTarget, Action{PerspexPropertyChangedEventArgs}})"/>.
|
|
/// </summary>
|
|
/// <typeparam name="TTarget">The sender type to accept.</typeparam>
|
|
/// <param name="e">The event args.</param>
|
|
/// <param name="handler">Given a TTarget, returns the handler.</param>
|
|
private static void SubscribeAdapter<TTarget>(
|
|
PerspexPropertyChangedEventArgs e,
|
|
Func<TTarget, Action<PerspexPropertyChangedEventArgs>> handler)
|
|
where TTarget : class
|
|
{
|
|
var target = e.Sender as TTarget;
|
|
|
|
if (target != null)
|
|
{
|
|
handler(target)(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|