Skip to content

Commit

Permalink
remove the strong reference carried from delegate (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
tian-lt authored May 17, 2021
1 parent af90e39 commit 27abae0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Calculator/Utils/DelegateCommandUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CalculatorApp.Utils
{
static class DelegateCommandUtils
{
public static DelegateCommand MakeDelegateCommand<TTarget>(TTarget target, DelegateCommandHandler handler)
public static DelegateCommand MakeDelegateCommand<TTarget>(TTarget target, Action<TTarget, object> handler)
where TTarget : class
{
WeakReference weakTarget = new WeakReference(target);
Expand All @@ -17,7 +17,7 @@ public static DelegateCommand MakeDelegateCommand<TTarget>(TTarget target, Deleg
TTarget thatTarget = weakTarget.Target as TTarget;
if(null != thatTarget)
{
handler.Invoke(param);
handler.Invoke(thatTarget, param);
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/Calculator/Views/Calculator.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ public System.Windows.Input.ICommand HistoryButtonPressed
{
if (donotuse_HistoryButtonPressed == null)
{
donotuse_HistoryButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, ToggleHistoryFlyout);
donotuse_HistoryButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.ToggleHistoryFlyout(param);
});
}
return donotuse_HistoryButtonPressed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public System.Windows.Input.ICommand ButtonPressed
{
if (donotuse_ButtonPressed == null)
{
donotuse_ButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnAngleButtonPressed);
donotuse_ButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param)=>
{
that.OnAngleButtonPressed(param);
});
}
return donotuse_ButtonPressed;
}
Expand Down
12 changes: 10 additions & 2 deletions src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public System.Windows.Input.ICommand ZoomOutButtonPressed
{
if (donotuse_ZoomOutButtonPressed == null)
{
donotuse_ZoomOutButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnZoomOutCommand);
donotuse_ZoomOutButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.OnZoomOutCommand(param);
});
}
return donotuse_ZoomOutButtonPressed;
}
Expand All @@ -128,7 +132,11 @@ public System.Windows.Input.ICommand ZoomInButtonPressed
{
if (donotuse_ZoomInButtonPressed == null)
{
donotuse_ZoomInButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnZoomInCommand);
donotuse_ZoomInButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.OnZoomInCommand(param);
});
}
return donotuse_ZoomInButtonPressed;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand All @@ -17,7 +17,11 @@ public ICommand BitLengthButtonPressed
{
if (donotuse_BitLengthButtonPressed == null)
{
donotuse_BitLengthButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnBitLengthButtonPressed);
donotuse_BitLengthButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.OnBitLengthButtonPressed(param);
});
}
return donotuse_BitLengthButtonPressed;
}
Expand Down

0 comments on commit 27abae0

Please sign in to comment.