如何转义 XAML 标记扩展属性文本中的单引号?
我有一个可以格式化数字的值转换器(不幸的是我还不能使用 SP1)。它工作正常,直到获得百分比。
下面是一个示例:
<TextBlock Text="{Binding Path=PercentageComplete,
Converter={StaticResource NumberFormatter},
ConverterParameter='0.00 %'}" />
不幸的是,当 Double.ToString
看到百分比字符时,它会将数字乘以 100。在我的例子中,数字已经是百分比,不需要转换。
在 C# 中,这可以通过使用单引号转义 %
字符来实现:
(99.99).ToString("0.00 %") // gives -> "9999 %"
(99.99).ToString("0.00 '%") // gives -> "99.99 %"
不幸的是,我无法在上述 XAML 标记扩展的 ConverterParameter
中使用单引号。有办法逃避吗?我尝试过加倍单引号并使用反斜杠,但都无法编译。
I have a value converter that formats numbers (I can't use SP1 yet unfortunately). It works fine until it gets a percentage.
Here's an example:
<TextBlock Text="{Binding Path=PercentageComplete,
Converter={StaticResource NumberFormatter},
ConverterParameter='0.00 %'}" />
Unfortunately for me when Double.ToString
sees a percentage character, it multiplies the number by 100. In my case, the number is already a percentage and no conversion is needed.
In C#, this would be achieved by escaping the %
character with a single quote:
(99.99).ToString("0.00 %") // gives -> "9999 %"
(99.99).ToString("0.00 '%") // gives -> "99.99 %"
Unfortunately, I cannot use a single quote in the ConverterParameter
in the above XAML markup extension. Is there a way of escaping it? I have tried doubling the single quotes and using a backslash, but both failed to compile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
未经测试,但您尝试过:
Untested, but have you tried:
以下内容摘自 http://msdn.microsoft.com/en-us/库/ee200269.aspx。
您可以使用反斜杠转义任何字符。它不会解析反斜杠,而只是跳过它们。因此:
'foo\'bar'
变为foo'bar
;"foo\"bar"
变为foo"bar
;'foo\\bar'
变为foo\bar
;'foo\nbar'
变为foonbar
而不是换行符。然而,标记扩展解析相当奇怪。尽管 Visual Studio 语法突出显示不支持它,但除
\{},=
之外的任何字符都是有效的值字符。这意味着以下结构是合法的:{MyExtension Name=foo'bar}
(引号必须是被视为引用字符串的第一个字符;其他任何地方都只是逐字复制);{MyExtension Name=f oo}
(空格也是合法的;这会变成f oo
);{MyExtension Name= foo }
(值周围的空格被修剪;这变成foo
);{MyExtension Name=foo\\bar}
(\
之后的字符被逐字复制,因此变成foo\bar
);{MyExtension Name=foo \\ bar}
(这变成foo \ bar
);{MyExtension Name=foo \} bar}
(这变成foo } bar
)。请注意,
\
规则也适用于此处:\
后面的任何字符都会逐字复制。The below is taken from http://msdn.microsoft.com/en-us/library/ee200269.aspx.
You can escape any character with a backslash. It does not parse the backslashes but just skips them. So:
'foo\'bar'
becomesfoo'bar
;"foo\"bar"
becomesfoo"bar
;'foo\\bar'
becomesfoo\bar
;'foo\nbar'
becomesfoonbar
and not a newline character.However, markup extension parsing is quite strange. Even though Visual Studio syntax highlighting doesn't support it, any character other than
\{},=
is a valid value character. This means that the following constructions are legal:{MyExtension Name=foo'bar}
(quotes must be the first character to be considered quoting for a string; anywhere else its just copied verbatim);{MyExtension Name=f oo}
(spaces are legal too; this becomesf oo
);{MyExtension Name= foo }
(spaces around the value are trimmed; this becomesfoo
);{MyExtension Name=foo\\bar}
(characters after a\
are copied verbatim, so this becomesfoo\bar
);{MyExtension Name=foo \\ bar}
(this becomesfoo \ bar
);{MyExtension Name=foo \} bar}
(and this becomesfoo } bar
).Note that the
\
rules apply here too: any character following a\
is copied verbatim.您可以使用 String.Format 而不是 Double.ToString
并且在绑定表达式中,使用特殊的 {} 转义序列:
You can use String.Format instead of Double.ToString
And in your binding expression, use the special {} escape sequence:
这是避免标记扩展的解决方法,尽管它不是问题的直接答案。
Here is a workaround that avoids the markup extension, though it isn't a direct answer to the question.