EventArgs to InvokeCommandAction
Hi,
How to pass EventArgs from EventTrigger to InvokeCommandAction?
I solved it by changing the NoesisApp::EventTriggerBase code by adding the GetEventArgs method
How to pass EventArgs from EventTrigger to InvokeCommandAction?
I solved it by changing the NoesisApp::EventTriggerBase code by adding the GetEventArgs method
Code: Select all
Noesis::EventArgs* EventTriggerBase::GetEventArgs() const;
Code: Select all
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseUp" x:Name="Trigger.Up">
<i:InvokeCommandAction Command="{Binding OnCommand}" CommandParameter="{Binding ElementName=Trigger.Up}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Code: Select all
void OnCommand(BaseComponent* param)
{
if (param && param->GetClassType() == TypeOf<NoesisApp::EventTrigger>())
{
auto trigger = (NoesisApp::EventTrigger*)param;
if (Symbol(trigger->GetEventName()) == Symbol("MouseUp")) {
auto args = (MouseButtonEventArgs*)trigger->GetEventArgs();
};
if (Symbol(trigger->GetEventName()) == Symbol("DragBegun")) {
auto args = (BehaviorDragEventArgs*)trigger->GetEventArgs();
};
}
}
Re: EventArgs to InvokeCommandAction
Improved the solution a bit.
Wrapped EventArgs in a component.
If CommandParameter is not specified in InvokeCommandAction, then EventTriggerArgsComponent* is passed by default
Wrapped EventArgs in a component.
Code: Select all
class NS_APP_INTERACTIVITY_API EventTriggerArgsComponent : public Noesis::BaseComponent
{
public:
Noesis::Symbol eventName;
Noesis::EventArgs* args;
NS_IMPLEMENT_INLINE_REFLECTION_(EventTriggerArgsComponent, Noesis::BaseComponent)
};
Code: Select all
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseUp">
<i:InvokeCommandAction Command="{Binding OmCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Code: Select all
void OnCommand(BaseComponent* param)
{
if (param && param->GetClassType() == TypeOf<NoesisApp::EventTriggerArgsComponent>())
{
auto component = (NoesisApp::EventTriggerArgsComponent*)param;
if (component->eventName == Symbol("MouseUp")) {
auto args = (MouseButtonEventArgs*)component->args;
};
}
}
-
-
sfernandez
Site Admin
- Posts: 2540
- Joined:
Re: EventArgs to InvokeCommandAction
Modifying EventTriggerBase seems the easiest way to achieve what you want (although will change behavior compared to WPF). You need to modify EventTriggerBase::OnEvent to receive an EventArgs so you can pass it from each event handler. Then you can just box the pointer to pass it to the invoked actions:
And then in InvokeCommandAction, as you said, if CommandParameter is null, then pass the parameter from the Invoke method:
This will allow your view model command to do the following:
Code: Select all
void EventTriggerBase::OnEvent(const Noesis::EventArgs* e)
{
InvokeActions(Noesis::Boxing::Box(e));
}
Code: Select all
void InvokeCommandAction::Invoke(BaseComponent* param)
{
if (GetAssociatedObject() != 0)
{
Noesis::ICommand* command = ResolveCommand();
BaseComponent* commandParameter = GetCommandParameter();
if (commandParameter != nullptr) param = commandParameter;
if (command != 0 && command->CanExecute(param))
{
command->Execute(param);
}
}
}
Code: Select all
void OnCommand(BaseComponent* param)
{
if (Boxing::CanUnbox<const EventArgs*>(param))
{
const EventArgs* e = Boxing::Unbox<const EventArgs*>(param);
// ...
}
}
-
-
sfernandez
Site Admin
- Posts: 2540
- Joined:
Re: EventArgs to InvokeCommandAction
You're welcome, marking this as solved.
Who is online
Users browsing this forum: Ahrefs [Bot] and 0 guests