Xamarin形式WinPhone - 如何使标签文本下划线WinPhone?
尝试使用以下xaml;
<StackLayout Orientation="Vertical">
<Label Text="SomeText"/>
<BoxView HeightRequest="1" HorizontalOptions="FillAndExpand" BackgroundColor="Black"/>
</StackLayout>
这应该为所有3个平台。 :)
这只是绘制一个高度为1的盒子视图,与下划线无关。当标签文本较短或者指定对齐方式以及进入两行时,它会中断。 –
我对文本属性长度上的数据触发器具有相同的实现,这为我提供了谷歌材质设计条目。直到现在我都很满意。 :) –
正如@RohitVipinMathews所说,这实际上只是一个拙劣的解决方法,可能会有问题。 – jbyrd
我认为您需要为此创建一个自定义视图,使其具有一个标签和一个带有小标签heightRequest的BoxView以充当一条线。
是的,我可以找不到另一个解决方案 –
你有最好的创建一个新的控件在你的PCL /共享项目继承自标签。
public class Exlabel : Label
{
}
在Windows Phone项目为它创建一个Custom Renderer如下,并使用TextBlock.TextDecorations属性设置下划线。 标签在Windows中呈现为TextBlock。
样品(未经测试):
[assembly: ExportRenderer(typeof(Exlabel), typeof(ExlabelRenderer))]
namespace CustomRenderer.WinPhone81
{
public class ExlabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.TextDecorations = TextDecorations.UnderLine;
}
}
}
}
如果您使用的是Windows手机看看这个样本 - How to format texts of TextBlock using xaml in Windows Phone。
对于WinRT,你可以使用这个 - TextBlock underline in WinRT。
在SilverLight WinPhone(旧的和不太支持的模板)中,您还可以使用保证金来实现您所需的功能,类似于How to make an underlined input text field in Windows Phone?。
我找不到此属性!我有这样的错误: 错误\t CS1061 \t“TextBlock的”不包含“TextDecorations”的定义,并没有扩展方法“TextDecorations”接受式“的TextBlock”的第一个参数可以找到(是否缺少using指令或程序组装参考?) –
您在Windows phone项目中获得Control的类型是什么? –
'TextBlock',你可以在我的错误信息中看到这个 –
在WinPhone项目中创建一个标签渲染:
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
[assembly: ExportRenderer(typeof(ExtendedLabel), typeof(ExtendedLabelRenderer))]
namespace SampleProject.WinPhone
{
public class ExtendedLabelRenderer: LabelRenderer
{
ExtendedLabel element;
TextBlock control;
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if((ExtendedLabel)Element == null || Control == null)
return;
element = (ExtendedLabel)Element;
control = Control;
UnderlineText();
}
void UnderlineText()
{
control.Text = string.Empty;
Underline ul = new Underline();
Run run = new Run();
run.Text = element.Text;
ul.Inlines.Add(run);
control.Inlines.Add(ul);
}
}
}
创建'Label'渲染器,并使用原生** ** WinPhone方法来创建带下划线的文本。 –
你见过[this](https://github.com/XLabs/Xamarin-Forms-Labs/wiki/ExtendedLabel)吗? –
是@EgorGromadskiy但我不能找到** ** ExtendedLabelRenderer为WinPhone在这[GitHub库(https://github.com/XLabs/Xamarin-Forms-Labs/wiki/ExtendedLabel#platform-code) –