【问题标题】:specifying icons in binding expression from code-behind file从代码隐藏文件中指定绑定表达式中的图标
【发布时间】:2020-11-19 09:43:10
【问题描述】:

在 NativeScript JavaScript Core 项目中,我有一个 GridLayout,我需要将它从静态定义的 XML 移动到从代码隐藏文件动态创建的。我有来自 XML 的这个特殊标签:

<Label class="{{ allSelected ? 'item-icon-undefined' : 'item-icon-selected' }}" 
 col="0" text="{{ allSelected ? '&#xf10c;' : '&#xf111;' }}" tap="onSelectAllTap" />

我的问题是文本属性。我一开始是:

Let selector = new Label; 
selector.bind({
  targetProperty: "text",
  sourceProperty: "$value",
  expression: "allSelected ? '&#xf10c;' : '&#xf111;' ",
}); 

但是文本显示的是字符串本身,比如好的,我之前打过这个,所以我添加了 charFromCode 的:

selector.bind({
  targetProperty: "text",
  sourceProperty: "$value",
  expression: "allSelected ? String.fromCharCode('0xf111') : String.fromCharCode('0xf10C') ",
}); 

但这会产生错误消息,

Error: Only identifier function invocations are allowed

好的,我想我需要将函数调用移到绑定之外,所以我使用了

  let circleSolid = String.fromCharCode('0xf111');
  let circleHollow = String.fromCharCode('0xf10C');
  selector.bind({
    targetProperty: "text",
    sourceProperty: "$value",
    expression: "allSelected ? circleHollow : circleSolid ",
  });

但是有了这个,没有错误消息,也没有输出。当我在调试时查看元素时,文本属性根本不存在。如果我绕过绑定并尝试

selector.text = circleSolid;

然后我得到了预期的图标。而且,如果我用表达式中的简单文本替换图标,那也可以。那么,如何在绑定表达式中指定图标值?

编辑 2020 年 7 月 29 日

我意识到这可能是一个范围界定问题,并且在最终评估绑定表达式时未定义变量。我把这两个let 改成了var 并把它们移到了构造函数的顶部,但是没有任何乐趣。

因此看起来我需要一个文字表达式,但是在这种情况下如何为 unicode 值完成呢? '' 之类的值和 '0xf10c' 已经失败了。我还认为正确转义字符串可能会更好,也许是这样,但是 '\'也不行。

几分钟后编辑 2020 年 7 月 29 日

这里的关键词是“逃避”。我输入了十六进制值作为转义的 unicode 字符(即'\uf10c'),一切都按预期工作。我会回答我自己的问题。

【问题讨论】:

    标签: data-binding icons nativescript code-behind


    【解决方案1】:

    请参阅上面的编辑。在这种情况下,图标代码点可以表示为转义的 unicode 字符,例如 '\uf10c'。

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 2013-08-08
      • 1970-01-01
      • 2013-01-24
      • 2010-11-12
      • 2012-10-16
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      相关资源
      最近更新 更多