Xamarin使用XAML设置Editor占位符时换行符失效问题求解
问题原因
直接在XAML属性的引号内写\n时,XAML解析器不会将其转义为换行符,只会作为普通字符输出;<br/>是HTML专属标签,XAML本身不识别该语法,因此两种写法都无法实现换行。
可行解决方案
方案1:使用XML换行字符实体
用XML规范的换行实体 代替\n即可直接在属性内实现换行:
<Editor Placeholder="This is one line this is the next one."/>
方案2:属性元素多行编写
如果占位符内容较长、行数较多,可以拆分为属性元素的形式直接换行编写内容:
注意:如果不需要保留每行开头的缩进空格,将内容顶格编写即可
<Editor> <Editor.Placeholder> This is one line this is the next one. </Editor.Placeholder> </Editor>
方案3:后台代码/绑定赋值
如果占位符是动态生成的,在C#代码中直接使用\n或者Environment.NewLine赋值即可,不需要额外转义:
// 后台控件直接赋值 myEditor.Placeholder = "This is one line \n this is the next one."; // MVVM绑定场景的ViewModel代码 public string EditorPlaceholder { get; set; } = "This is one line \n this is the next one.";
内容的提问来源于stack exchange,提问作者Flimtix
相关产品推荐
相关产品推荐

