Skip to content

Commit

Permalink
Merge pull request #29 from wieslawsoltes/CustomReactiveBehaviors
Browse files Browse the repository at this point in the history
Add new custom reactive event behaviors
  • Loading branch information
wieslawsoltes authored Jan 4, 2025
2 parents e51a1b0 + 0d53d64 commit cc18c1b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Reactive.Disposables;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// A base class for behaviors using ActualThemeVariantChanged event.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class ActualThemeVariantChangedBehavior<T> : DisposingBehavior<T> where T : StyledElement
{
private CompositeDisposable? _disposables;

/// <inheritdoc />
protected override void OnAttached(CompositeDisposable disposables)
{
_disposables = disposables;
}

/// <inheritdoc />
protected override void OnActualThemeVariantChangedEvent()
{
OnActualThemeVariantChangedEvent(_disposables!);
}

/// <summary>
/// Called when the <see cref="StyledElementBehavior{T}.AssociatedObject"/> ActualThemeVariantChanged event is raised.
/// </summary>
/// <param name="disposable">The group of disposable resources that are disposed together</param>
protected abstract void OnActualThemeVariantChangedEvent(CompositeDisposable disposable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Reactive.Disposables;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// A base class for behaviors using DataContextChanged event.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class DataContextChangedBehavior<T> : DisposingBehavior<T> where T : StyledElement
{
private CompositeDisposable? _disposables;

/// <inheritdoc />
protected override void OnAttached(CompositeDisposable disposables)
{
_disposables = disposables;
}

/// <inheritdoc />
protected override void OnDataContextChangedEvent()
{
OnDataContextChangedEvent(_disposables!);
}

/// <summary>
/// Called when the <see cref="StyledElementBehavior{T}.AssociatedObject"/> DataContextChanged event is raised.
/// </summary>
/// <param name="disposable">The group of disposable resources that are disposed together</param>
protected abstract void OnDataContextChangedEvent(CompositeDisposable disposable);
}
31 changes: 31 additions & 0 deletions src/Avalonia.Xaml.Interactions.Custom/Core/InitializedBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Reactive.Disposables;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// A base class for behaviors using Initialized event.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class InitializedBehavior<T> : DisposingBehavior<T> where T : StyledElement
{
private CompositeDisposable? _disposables;

/// <inheritdoc />
protected override void OnAttached(CompositeDisposable disposables)
{
_disposables = disposables;
}

/// <inheritdoc />
protected override void OnInitializedEvent()
{
OnInitializedEvent(_disposables!);
}

/// <summary>
/// Called when the <see cref="StyledElementBehavior{T}.AssociatedObject"/> Initialized event is raised.
/// </summary>
/// <param name="disposable">The group of disposable resources that are disposed together</param>
protected abstract void OnInitializedEvent(CompositeDisposable disposable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Reactive.Disposables;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// A base class for behaviors using ResourcesChanged event.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class ResourcesChangedBehavior<T> : DisposingBehavior<T> where T : StyledElement
{
private CompositeDisposable? _disposables;

/// <inheritdoc />
protected override void OnAttached(CompositeDisposable disposables)
{
_disposables = disposables;
}

/// <inheritdoc />
protected override void OnResourcesChangedEvent()
{
OnResourcesChangedEvent(_disposables!);
}

/// <summary>
/// Called when the <see cref="StyledElementBehavior{T}.AssociatedObject"/> ResourcesChanged event is raised.
/// </summary>
/// <param name="disposable">The group of disposable resources that are disposed together</param>
protected abstract void OnResourcesChangedEvent(CompositeDisposable disposable);
}

0 comments on commit cc18c1b

Please sign in to comment.