WPF和字符串格式化 [英] WPF and string formatting

查看:251
本文介绍了WPF和字符串格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的一些XAML:

 < Window.Resources> 
< v:MyClass x:Key =whateverText =foo\\\
bar/>
< /Window.Resources>

显然,我想在MyClass.Text属性中使用换行符,但XAML解析器使用文字字符串foo\\\
bar。



是否有(a)说服解析器翻译转义序列的方法,或(b).NET方法以C#编译器的方式解释一个字符串?



我意识到我可以去那里寻找 \\\
sequence,但是有一个通用的方式可以做到这一点。

解决方案

可以使用XML字符转义

 < TextBlock Text =Hello&#13; World!/> 


Suppose I have some XAML like this:

<Window.Resources>
  <v:MyClass x:Key="whatever" Text="foo\nbar" />
</Window.Resources>

Obviously I want a newline character in the MyClass.Text property, but the XAML parser constructs the object with the literal string "foo\nbar".

Is there (a) a way to convince the parser to translate escape sequences, or (b) a .NET method to interpret a string in the way that the C# compiler would?

I realize that I can go in there looking for \n sequences, but it would be nicer to have a generic way to do this.

解决方案

You can use XML character escaping

<TextBlock Text="Hello&#13;World!"/>

这篇关于WPF和字符串格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆