remove the strong reference carried from delegate (#32)

This commit is contained in:
Tian L 2021-05-17 16:50:25 +08:00 committed by GitHub
commit 27abae0099
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 8 deletions

View file

@ -8,7 +8,7 @@ namespace CalculatorApp.Utils
{ {
static class DelegateCommandUtils 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 where TTarget : class
{ {
WeakReference weakTarget = new WeakReference(target); WeakReference weakTarget = new WeakReference(target);
@ -17,7 +17,7 @@ namespace CalculatorApp.Utils
TTarget thatTarget = weakTarget.Target as TTarget; TTarget thatTarget = weakTarget.Target as TTarget;
if(null != thatTarget) if(null != thatTarget)
{ {
handler.Invoke(param); handler.Invoke(thatTarget, param);
} }
}); });
} }

View file

@ -141,7 +141,11 @@ namespace CalculatorApp
{ {
if (donotuse_HistoryButtonPressed == null) if (donotuse_HistoryButtonPressed == null)
{ {
donotuse_HistoryButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, ToggleHistoryFlyout); donotuse_HistoryButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.ToggleHistoryFlyout(param);
});
} }
return donotuse_HistoryButtonPressed; return donotuse_HistoryButtonPressed;
} }

View file

@ -48,7 +48,11 @@ namespace CalculatorApp
{ {
if (donotuse_ButtonPressed == null) if (donotuse_ButtonPressed == null)
{ {
donotuse_ButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnAngleButtonPressed); donotuse_ButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param)=>
{
that.OnAngleButtonPressed(param);
});
} }
return donotuse_ButtonPressed; return donotuse_ButtonPressed;
} }

View file

@ -115,7 +115,11 @@ namespace CalculatorApp
{ {
if (donotuse_ZoomOutButtonPressed == null) if (donotuse_ZoomOutButtonPressed == null)
{ {
donotuse_ZoomOutButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnZoomOutCommand); donotuse_ZoomOutButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.OnZoomOutCommand(param);
});
} }
return donotuse_ZoomOutButtonPressed; return donotuse_ZoomOutButtonPressed;
} }
@ -128,7 +132,11 @@ namespace CalculatorApp
{ {
if (donotuse_ZoomInButtonPressed == null) if (donotuse_ZoomInButtonPressed == null)
{ {
donotuse_ZoomInButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnZoomInCommand); donotuse_ZoomInButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.OnZoomInCommand(param);
});
} }
return donotuse_ZoomInButtonPressed; return donotuse_ZoomInButtonPressed;
} }

View file

@ -1,4 +1,4 @@
using System.Diagnostics; using System.Diagnostics;
using System.Windows.Input; using System.Windows.Input;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
@ -17,7 +17,11 @@ namespace CalculatorApp
{ {
if (donotuse_BitLengthButtonPressed == null) if (donotuse_BitLengthButtonPressed == null)
{ {
donotuse_BitLengthButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnBitLengthButtonPressed); donotuse_BitLengthButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this,
(that, param) =>
{
that.OnBitLengthButtonPressed(param);
});
} }
return donotuse_BitLengthButtonPressed; return donotuse_BitLengthButtonPressed;
} }