problem in Register command
I want to register a custom command in UserControl, I implement it in WPF, but not work C++, some advices?
Code: Select all
//command in WPF
public static readonly DependencyProperty ButtonActionCommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(TouchButton), new UIPropertyMetadata(null));
public ICommand Command
{
get { return (ICommand)GetValue(ButtonActionCommandProperty); }
set { SetValue(ButtonActionCommandProperty, value); }
}
Code: Select all
//Register command
/*UIData->RegisterProperty<Ptr<BaseCommand>>(CommandProperty, "Command",
FrameworkPropertyMetadata::Create(Ptr<BaseCommand>(), FrameworkOptions_None));*/
UIData->RegisterProperty<Ptr<BaseCommand>>(CommandProperty, "Command",
UIPropertyMetadata::Create(Ptr<BaseCommand>()));
/*UIData->RegisterProperty<Ptr<BaseComponent>>(CommandParameterProperty, "CommandParameter",
FrameworkPropertyMetadata::Create(Ptr<BaseComponent>(), FrameworkOptions_None));*/
UIData->RegisterProperty<Ptr<BaseComponent>>(CommandParameterProperty, "CommandParameter",
UIPropertyMetadata::Create(Ptr<BaseComponent>()));
//Implemtent ICommand interface
Noesis::ICommand* TouchButton::GetCommand() const
{
//return nullptr;
return DependencyObject::GetValue<Ptr<ICommand>>(CommandProperty).GetPtr();
}
void GameGUI::TouchButton::SetCommand(BaseCommand* InCommand)
{
DependencyObject::SetValue<Ptr<BaseCommand>>(CommandProperty, InCommand);
}
Noesis::BaseComponent* TouchButton::GetCommandParameter() const
{
return DependencyObject::GetValue<Ptr<BaseComponent>>(CommandParameterProperty).GetPtr();
}
void TouchButton::SetCommandParameter(BaseComponent* InParam)
{
DependencyObject::SetValue<Ptr<BaseComponent>>(CommandParameterProperty, InParam);
}
//Fire command
if (GetCommand())
{
//this line is called
GetCommand()->Execute(GetCommandParameter());
}
Re: problem in Register command
Sorry for the late reply. What's exactly the problem? The only thing that I see missing is registering the property itself in your reflection code, like this:
Can you give that a try and let me know if that fixes it for you?
Code: Select all
NsProp("Command", & TouchButton::GetCommand, & TouchButton::SetCommand)
.Meta<TypeConverterMetaData>("Converter<Command>");
Re: problem in Register command
I'm sorry, one of my colleagues has pointed out an error in your code. The types in the Get/Set functions are wrong. Try this:
Note that the type used is a BaseComponent, that is later cast to ICommand.
Code: Select all
// Reflection code
NsProp("Command", &TouchButton::GetCommand, &TouchButton::SetCommand)
.Meta<TypeConverterMetaData>("Converter<Command>");
Ptr<UIElementData> data = NsM88eta<UIElementData>(TypeOf<SelfClass>());
data->RegisterProperty<Ptr<BaseComponent> >(CommandProperty, "Command",
FrameworkPropertyMetadata::Create(Ptr<BaseComponent>(), FrameworkOptions_None));
data->RegisterProperty<Ptr<BaseComponent> >(CommandParameterProperty, "CommandParameter",
FrameworkPropertyMetadata::Create(Ptr<BaseComponent>(), FrameworkOptions_None));
// Getter
ICommand* TouchButton::GetCommand() const
{
return NsDynamicCast<ICommand*>(GetValue<Ptr<BaseComponent> >(CommandProperty).GetPtr());
}
// Setter
void TouchButton::SetCommand(ICommand* command)
{
SetValue<Ptr<BaseComponent> >(CommandProperty, command != 0 ?
NsStaticCast<BaseComponent*>(command->GetBaseObject()) : 0);
}
-
sfernandez
Site Admin
- Posts: 2995
- Joined:
Re: problem in Register command
To simplify this code a lot you can derive your command from BaseCommand and make the property use the base type instead of BaseComponent:
I want to remark here that adding the property to reflection (NsProp) to register the Command converter is not necessary unless you want that property to be able to parse ApplicationCommands and ComponentCommands from xaml.
The property getter and setter will use both BaseCommand also:
Then you can execute the command like this:
Code: Select all
data->RegisterProperty<Ptr<BaseCommand>>(CommandProperty, "Command", PropertyMetadata::Create(Ptr<BaseCommand>()));
The property getter and setter will use both BaseCommand also:
Code: Select all
BaseCommand* TouchButton::GetCommand() const
{
return GetValue<Ptr<BaseCommand>>(CommandProperty);
}
void TouchButton::SetCommand(BaseCommand* command)
{
SetValue<Ptr<BaseCommand>>(CommandProperty, command);
}
Code: Select all
BaseCommand* command = GetCommand();
BaseComponent* param = GetCommandParameter();
if (command->CanExecute(param))
{
command->Execute(param);
}
Re: problem in Register command
You welcome! Marking as solved
Re: problem in Register command
Can you make more explanation about NsProp?
the Command or BaseCommand is a DependencyProperty, same as WidthProperty? BackgroundProperty?
why does Command need a NsProp? why doesn't others?
the Command or BaseCommand is a DependencyProperty, same as WidthProperty? BackgroundProperty?
why does Command need a NsProp? why doesn't others?
Re: problem in Register command
If you want to support built-in commands, then you need to add a converter for the property. For example, to properly convert the string "ApplicationCommands.Copy" to a valid command. Right now, we only support adding converters to reflection properties, not to dependencies properties. This is exactly the same as in C#, and probably a bit confusing. You can avoid it at all, I don't think you need it.
Code: Select all
NsProp("Command", &TouchButton::GetCommand, &TouchButton::SetCommand)
.Meta<TypeConverterMetaData>("Converter<Command>");
Who is online
Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 22 guests