如何将数组绑定到自定义索引器?
How to bind an array to custom indexer?
我有一个带索引器的单例本地化 class。您可以这样访问它:
Localize.Current[String key]
我还有一个自定义绑定 class,它将 string
转换为对此索引器的调用:
{Translate MyKey}
这将在我的绑定上设置模式 (OneWay
),源为 Localize.Current
(单例),路径为:
[$key$]
其中 $key$
的计算结果为 MyKey
。然后将其本地化并替换为本地化值。
现在我想用额外的参数来参数化这个绑定。所以我这样做了:
Localize.Current[String key, params String[] parameters]
{Translate MyKey, MyParameter1, MyParameter2}
我不知道如何在绑定中设置路径,我试过:
new PropertyPath("[$key$, (0)]", values) // where values is String[] from Translate
这不会触发索引器。如果我添加一个但不是任意数量的参数,它会触发 Localize.Current[String key, String parameter]
索引器。我需要一个包含对数组的引用的路径,例如:
new PropertyPath("[$key$, $array_placeholder$], values)
到处都找不到任何东西。既不在文档中,也不在 Google 中。任何人都有如何实现这一目标的经验?用什么代替 $array_placeholder$
?
带路径和参数的PropertyPath
constructor是这样定义的
public PropertyPath (string path, params object[] pathParameters);
pathParameters
参数是一个数组,其中包含 path
中每个索引的 object
。这意味着为了将 values
array 传递给 属性 路径中的索引零,它必须是 中的第一个值参数数组。然后,将调用适当的索引器。
new PropertyPath("[$key$, (0)]", new object[] {values});
如果直接传递values
,路径中的索引零将获得该数组的第一个值,一个string
.
我有一个带索引器的单例本地化 class。您可以这样访问它:
Localize.Current[String key]
我还有一个自定义绑定 class,它将 string
转换为对此索引器的调用:
{Translate MyKey}
这将在我的绑定上设置模式 (OneWay
),源为 Localize.Current
(单例),路径为:
[$key$]
其中 $key$
的计算结果为 MyKey
。然后将其本地化并替换为本地化值。
现在我想用额外的参数来参数化这个绑定。所以我这样做了:
Localize.Current[String key, params String[] parameters]
{Translate MyKey, MyParameter1, MyParameter2}
我不知道如何在绑定中设置路径,我试过:
new PropertyPath("[$key$, (0)]", values) // where values is String[] from Translate
这不会触发索引器。如果我添加一个但不是任意数量的参数,它会触发 Localize.Current[String key, String parameter]
索引器。我需要一个包含对数组的引用的路径,例如:
new PropertyPath("[$key$, $array_placeholder$], values)
到处都找不到任何东西。既不在文档中,也不在 Google 中。任何人都有如何实现这一目标的经验?用什么代替 $array_placeholder$
?
带路径和参数的PropertyPath
constructor是这样定义的
public PropertyPath (string path, params object[] pathParameters);
pathParameters
参数是一个数组,其中包含 path
中每个索引的 object
。这意味着为了将 values
array 传递给 属性 路径中的索引零,它必须是 中的第一个值参数数组。然后,将调用适当的索引器。
new PropertyPath("[$key$, (0)]", new object[] {values});
如果直接传递values
,路径中的索引零将获得该数组的第一个值,一个string
.