Skip to content

Commit

Permalink
fixes EETypeLoadException issue: export class DelegateCommand (#18)
Browse files Browse the repository at this point in the history
* fixes EETypeLoadException issue: export class DelegateCommand

* weak-reference in C++/CX

* WeakRef in C# codebase

* UTF-8-BOM

* spaces in macro

* resolve some comments from the offline review

* format

* rename file
  • Loading branch information
tian-lt authored Apr 22, 2021
1 parent 5a84239 commit b53600b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 73 deletions.
52 changes: 26 additions & 26 deletions src/CalcViewModel/Common/DelegateCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ namespace CalculatorApp
{
namespace Common
{
template <typename TTarget>
ref class DelegateCommand : public Windows::UI::Xaml::Input::ICommand
{
internal :

typedef void (TTarget::*CommandHandlerFunc)(Platform::Object ^);
public delegate void DelegateCommandHandler(Platform::Object ^ parameter);

DelegateCommand(TTarget ^ target, CommandHandlerFunc func)
: m_weakTarget(target)
, m_function(func)
{
}
public ref class DelegateCommand sealed : public Windows::UI::Xaml::Input::ICommand
{
public:
DelegateCommand(DelegateCommandHandler ^ handler)
: m_handler(handler)
{}

private:
// Explicit, and private, implementation of ICommand, this way of programming makes it so
Expand All @@ -27,21 +23,17 @@ namespace CalculatorApp
// code in the app calling Execute.
virtual void ExecuteImpl(Platform::Object ^ parameter) sealed = Windows::UI::Xaml::Input::ICommand::Execute
{
TTarget ^ target = m_weakTarget.Resolve<TTarget>();
if (target)
{
(target->*m_function)(parameter);
}
m_handler->Invoke(parameter);
}

virtual bool CanExecuteImpl(Platform::Object ^ parameter) sealed = Windows::UI::Xaml::Input::ICommand::CanExecute
{
return true;
}

virtual event Windows::Foundation::EventHandler<Platform::Object^>^ CanExecuteChangedImpl
virtual event Windows::Foundation::EventHandler<Platform::Object ^> ^ CanExecuteChangedImpl
{
virtual Windows::Foundation::EventRegistrationToken add(Windows::Foundation::EventHandler<Platform::Object^>^ handler) sealed = Windows::UI::Xaml::Input::ICommand::CanExecuteChanged::add
virtual Windows::Foundation::EventRegistrationToken add(Windows::Foundation::EventHandler<Platform::Object ^> ^ handler) sealed = Windows::UI::Xaml::Input::ICommand::CanExecuteChanged::add
{
return m_canExecuteChanged += handler;
}
Expand All @@ -52,17 +44,25 @@ namespace CalculatorApp
}

private:
DelegateCommandHandler ^ m_handler;

event Windows::Foundation::EventHandler<Platform::Object^>^ m_canExecuteChanged;

CommandHandlerFunc m_function;
Platform::WeakReference m_weakTarget;
event Windows::Foundation::EventHandler<Platform::Object ^> ^ m_canExecuteChanged;
};

template <typename TTarget, typename TFuncPtr>
DelegateCommand<TTarget> ^ MakeDelegate(TTarget ^ target, TFuncPtr&& function) {
return ref new DelegateCommand<TTarget>(target, std::forward<TFuncPtr>(function));
}

DelegateCommandHandler ^ MakeDelegateCommandHandler(TTarget ^ target, TFuncPtr&& function)
{
Platform::WeakReference weakTarget(target);
return ref new DelegateCommandHandler([weakTarget, function=std::forward<TFuncPtr>(function)](Platform::Object ^ param)
{
TTarget ^ thatTarget = weakTarget.Resolve<TTarget>();
if (nullptr != thatTarget)
{
(thatTarget->*function)(param);
}
}
);
}
}
}

21 changes: 16 additions & 5 deletions src/CalcViewModel/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,22 @@ public:
// The variable member generated by this macro should not be used in the class code, use the
// property getter instead.
#define COMMAND_FOR_METHOD(p, m) \
property Windows::UI::Xaml::Input::ICommand^ p {\
Windows::UI::Xaml::Input::ICommand^ get() {\
if (!donotuse_##p) {\
donotuse_##p = CalculatorApp::Common::MakeDelegate(this, &m);\
} return donotuse_##p; }} private: Windows::UI::Xaml::Input::ICommand^ donotuse_##p; \
property Windows::UI::Xaml::Input::ICommand ^ p \
{ \
Windows::UI::Xaml::Input::ICommand ^ get() \
{ \
if (!donotuse_##p) \
{ \
donotuse_##p = ref new CalculatorApp::Common::DelegateCommand( \
CalculatorApp::Common::MakeDelegateCommandHandler(this, &m) \
); \
} \
return donotuse_##p; \
} \
} \
\
private: \
Windows::UI::Xaml::Input::ICommand ^ donotuse_##p; \
\
public:

Expand Down
2 changes: 1 addition & 1 deletion src/Calculator/Calculator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@
<Compile Include="Converters\ItemSizeToVisibilityConverter.cs" />
<Compile Include="Converters\RadixToStringConverter.cs" />
<Compile Include="Converters\VisibilityNegationConverter.cs" />
<Compile Include="DelegateCommand.cs" />
<Compile Include="EquationStylePanelControl.xaml.cs">
<DependentUpon>EquationStylePanelControl.xaml</DependentUpon>
</Compile>
<Compile Include="KeyGraphFeaturesTemplateSelector.cs" />
<Compile Include="Utils\DelegateCommandUtils.cs" />
<Compile Include="Views\Calculator.xaml.cs">
<DependentUpon>Calculator.xaml</DependentUpon>
</Compile>
Expand Down
35 changes: 0 additions & 35 deletions src/Calculator/DelegateCommand.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/Calculator/Utils/DelegateCommandUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using CalculatorApp.Common;

namespace CalculatorApp.Utils
{
static class DelegateCommandUtils
{
public static DelegateCommand MakeDelegateCommand<TTarget>(TTarget target, DelegateCommandHandler handler)
where TTarget : class
{
WeakReference weakTarget = new WeakReference(target);
return new DelegateCommand(param =>
{
TTarget thatTarget = weakTarget.Target as TTarget;
if(null != thatTarget)
{
handler.Invoke(param);
}
});
}
}
}

3 changes: 2 additions & 1 deletion src/Calculator/Views/Calculator.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using CalculatorApp.Common;
using CalculatorApp.Converters;
using CalculatorApp.Controls;
using CalculatorApp.Utils;
using CalculatorApp.ViewModel;

using Windows.Foundation;
Expand Down Expand Up @@ -140,7 +141,7 @@ public System.Windows.Input.ICommand HistoryButtonPressed
{
if (donotuse_HistoryButtonPressed == null)
{
donotuse_HistoryButtonPressed = new DelegateCommand<Calculator>(this, ToggleHistoryFlyout);
donotuse_HistoryButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, ToggleHistoryFlyout);
}
return donotuse_HistoryButtonPressed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using System.Diagnostics;
using CalculatorApp;
using CalculatorApp.Utils;
using CalculatorApp.ViewModel;

using Windows.Foundation;
Expand Down Expand Up @@ -47,7 +48,7 @@ public System.Windows.Input.ICommand ButtonPressed
{
if (donotuse_ButtonPressed == null)
{
donotuse_ButtonPressed = new CalculatorApp.Common.DelegateCommand<CalculatorScientificAngleButtons>(this, OnAngleButtonPressed);
donotuse_ButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnAngleButtonPressed);
}
return donotuse_ButtonPressed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using CalculatorApp.Common;
using CalculatorApp.Common.Automation;
using CalculatorApp.Controls;
using CalculatorApp.Utils;
using CalculatorApp.ViewModel;
//using CalcManager.NumberFormattingUtils;
using GraphControl;
Expand Down Expand Up @@ -114,7 +115,7 @@ public System.Windows.Input.ICommand ZoomOutButtonPressed
{
if (donotuse_ZoomOutButtonPressed == null)
{
donotuse_ZoomOutButtonPressed = new DelegateCommand<GraphingCalculator>(this, OnZoomOutCommand);
donotuse_ZoomOutButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnZoomOutCommand);
}
return donotuse_ZoomOutButtonPressed;
}
Expand All @@ -127,7 +128,7 @@ public System.Windows.Input.ICommand ZoomInButtonPressed
{
if (donotuse_ZoomInButtonPressed == null)
{
donotuse_ZoomInButtonPressed = new DelegateCommand<GraphingCalculator>(this, OnZoomInCommand);
donotuse_ZoomInButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnZoomInCommand);
}
return donotuse_ZoomInButtonPressed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using CalculatorApp.Utils;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

Expand All @@ -16,8 +17,7 @@ public ICommand BitLengthButtonPressed
{
if (donotuse_BitLengthButtonPressed == null)
{
donotuse_BitLengthButtonPressed =
new Common.DelegateCommand<CalculatorProgrammerDisplayPanel>(this, OnBitLengthButtonPressed);
donotuse_BitLengthButtonPressed = DelegateCommandUtils.MakeDelegateCommand(this, OnBitLengthButtonPressed);
}
return donotuse_BitLengthButtonPressed;
}
Expand Down

0 comments on commit b53600b

Please sign in to comment.