Unify dispose pattern in Behaviours
MODIFY
WalletWasabi.Gui/Behaviors/ClearPropertyOnLostFocusBehavior.cs
WalletWasabi.Gui/Behaviors/ClearPropertyOnLostFocusBehavior.cs
@@ -11,7 +11,7 @@ namespace WalletWasabi.Gui.Behaviors
{
public class ClearPropertyOnLostFocusBehavior : Behavior<Control>
{
- private CompositeDisposable _disposables;
+ private CompositeDisposable Disposables { get; set; }
public static readonly AvaloniaProperty<object> TargetPropertyProperty =
AvaloniaProperty.Register<ClearPropertyOnLostFocusBehavior, object>(nameof(TargetProperty), defaultBindingMode: BindingMode.TwoWay);
@@ -24,7 +24,7 @@ namespace WalletWasabi.Gui.Behaviors
protected override void OnAttached()
{
- _disposables = new CompositeDisposable
+ Disposables = new CompositeDisposable
{
Observable.FromEventPattern<RoutedEventArgs>(AssociatedObject, nameof(AssociatedObject.LostFocus)).Subscribe(args=>
{
@@ -37,9 +37,9 @@ namespace WalletWasabi.Gui.Behaviors
protected override void OnDetaching()
{
- _disposables?.Dispose();
-
base.OnDetaching();
+
+ Disposables?.Dispose();
}
}
}

MODIFY
WalletWasabi.Gui/Behaviors/CommandOnClickBehavior.cs
WalletWasabi.Gui/Behaviors/CommandOnClickBehavior.cs
@@ -6,15 +6,15 @@ namespace WalletWasabi.Gui.Behaviors
{
public class CommandOnClickBehavior : CommandBasedBehavior<InputElement>
{
- private CompositeDisposable _disposables;
+ private CompositeDisposable Disposables { get; set; }
protected override void OnAttached()
{
- _disposables = new CompositeDisposable();
+ Disposables = new CompositeDisposable();
base.OnAttached();
- _disposables.Add(AssociatedObject.AddHandler(InputElement.PointerPressedEvent, (sender, e) =>
+ Disposables.Add(AssociatedObject.AddHandler(InputElement.PointerPressedEvent, (sender, e) =>
{
e.Handled = ExecuteCommand();
}));
@@ -24,7 +24,7 @@ namespace WalletWasabi.Gui.Behaviors
{
base.OnDetaching();
- _disposables.Dispose();
+ Disposables?.Dispose();
}
}
}

MODIFY
WalletWasabi.Gui/Behaviors/CommandOnEnterBehavior.cs
WalletWasabi.Gui/Behaviors/CommandOnEnterBehavior.cs
@@ -5,15 +5,15 @@ namespace WalletWasabi.Gui.Behaviors
{
public class CommandOnEnterBehavior : CommandBasedBehavior<TextBox>
{
- private CompositeDisposable _disposables;
+ private CompositeDisposable Disposables { get; set; }
protected override void OnAttached()
{
- _disposables = new CompositeDisposable();
+ Disposables = new CompositeDisposable();
base.OnAttached();
- _disposables.Add(AssociatedObject.AddHandler(TextBox.KeyDownEvent, (sender, e) =>
+ Disposables.Add(AssociatedObject.AddHandler(TextBox.KeyDownEvent, (sender, e) =>
{
if (e.Key == Avalonia.Input.Key.Enter)
{
@@ -26,7 +26,7 @@ namespace WalletWasabi.Gui.Behaviors
{
base.OnDetaching();
- _disposables.Dispose();
+ Disposables.Dispose();
}
}
}

MODIFY
WalletWasabi.Gui/Behaviors/PasteAddressOnClickBehavior.cs
WalletWasabi.Gui/Behaviors/PasteAddressOnClickBehavior.cs
@@ -84,7 +84,9 @@ namespace WalletWasabi.Gui.Behaviors
protected override void OnAttached()
{
_originalToolTipText = (string)ToolTip.GetTip(AssociatedObject);
+
Disposables?.Dispose();
+
Disposables = new CompositeDisposable
{
AssociatedObject.GetObservable(TextBox.IsFocusedProperty).Subscribe(focused =>

MODIFY
WalletWasabi.Gui/Behaviors/SuggestionBehavior.cs
WalletWasabi.Gui/Behaviors/SuggestionBehavior.cs
@@ -12,7 +12,7 @@ namespace WalletWasabi.Gui.Behaviors
{
internal class SuggestionBehavior : Behavior<TextBox>
{
- private CompositeDisposable _disposables;
+ private CompositeDisposable Disposables { get; set; }
private static readonly AvaloniaProperty<IEnumerable<SuggestionViewModel>> SuggestionItemsProperty =
AvaloniaProperty.Register<SuggestionBehavior, IEnumerable<SuggestionViewModel>>(nameof(SuggestionItems), defaultBindingMode: Avalonia.Data.BindingMode.TwoWay);
@@ -25,11 +25,11 @@ namespace WalletWasabi.Gui.Behaviors
protected override void OnAttached()
{
- _disposables = new CompositeDisposable();
+ Disposables = new CompositeDisposable();
base.OnAttached();
- _disposables.Add(AssociatedObject.AddHandler(TextBox.KeyDownEvent, (sender, e) =>
+ Disposables.Add(AssociatedObject.AddHandler(TextBox.KeyDownEvent, (sender, e) =>
{
if (e.Key == Avalonia.Input.Key.Tab)
{
@@ -100,12 +100,12 @@ namespace WalletWasabi.Gui.Behaviors
{
base.OnDetaching();
- _disposables.Dispose();
+ Disposables?.Dispose();
}
private void HandleAutoUpdate()
{
- SuggestionItems.FirstOrDefault()?.OnSelected();
+ SuggestionItems?.FirstOrDefault()?.OnSelected();
}
}
}

Unify dispose pattern in Behaviours