Kanzi编程基础2 - Kanzi节点读取和属性设置

UI设计师在Kanzi studio把Kanzi的节点做好后,就要编码读取这些节点并根据实际功能去控制刷新它。

Kanzi读取节点的api发生过很多次变化,从2.7、2.8到3.0,每次变化都比较大,可能是因为kanzi引擎的设计思路还不是非常确定。

目前3.3版本可以通过application下的screen里的方法读取,如下:

std::shared_ptr<Node> spNode = app->getScreen()->lookupNode<Node>("RootPage/MainView/RPMGauge/Scene/RPM");

lookuoNode是一个模板方法,需要传入查找的节点的类型,我们可以使用基类型Node(Kanzi所有的节点的基类为Node),然后传入相对于Screen节点的路径。同样,也可以传入一个alias,如果使用的是alias,则要加前缀'#',如"#RPM"(名为RPM的alias需要在Kanzi studio中创建)。

该方法获取到的是该节点的一个智能指针,获取到后就可以通过这个智能指针就可以设置该节点的属性,如可见性、可用性等。

目前可以使用两种方式来设置kanzi节点的属性,

第1种:

使用Node类中的具体方法设置,如里面有setVisible方法,我们可以查找到这个方法的定义

void setVisible(bool value) { setProperty(VisibleProperty, value); }
如spNode->setVisible(false);则设置该节点隐藏。

Node类中定义了很多类似的方法,具体可以查看对应的头文件。

第2种:

使用Node类中的setProperty方法设置,传入属性的名称和值即可。具体的定义如下:

  /// Sets the local value of a property.
///
/// \param propertyType The property type identifying the property to set.
/// \param value The value to set.
template <typename DataType>
void setProperty(const PropertyType<DataType>& propertyType, typename PropertyType<DataType>::DataType value)
{
propertyType.setter(getPropertyManager(), this, value);
}

可以看得出来,这个方法比较灵活,只要知道属性的定义,就可以设置所有的属性(包括自定义的属性)。

同样,对应有getProperty方法可以获取所有的属性值。

 /// Returns the current value of a property.
///
/// The value returned by this function is the result of the property system evaluating the inputs that can affect the values of properties.
/// The final value is calculated by determining the base value of the property and applying existing modifiers to it.
///
/// Base value is affected by the following inputs where the highest entry in the list determines the base value:
/// 1. Local value set with setProperty or loaded from kzb
/// 2. Value set by a style affecting the property.
/// 3. Value defined by class metadata.
///
/// When the base value is determined the system applies modifiers to the value that can change the value or replace it completely.
/// The following is the list of possible modifiers, where the order of evaluation is determined by the order the modifiers were added or applied.
/// 1. Values defined is states of state manager.
/// 2. Animations.
///
/// If no inputs to the property value can be established the system returns the value registered in the property type metadata.
/// \param propertyType The property type identifying the property to retrieve.
/// \return Returns the evaluated property value.
template <typename DataType>
DataType getProperty(const PropertyType<DataType>& propertyType) const
{
return propertyType.getter(getPropertyManager(), this);
}

基本的节点获取就是那么简单。当然,深入做下去的时候,还会发现满足不了需求的情况,后面的进阶部分会继续介绍。

上一篇:201521123026 《JAVA程序设计》第12周学习总结


下一篇:tc/traffic control 网络控制工具