Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Xamarin表单中为具有下划线效果的标签加下划线?_Xamarin_Xamarin.forms - Fatal编程技术网

如何在Xamarin表单中为具有下划线效果的标签加下划线?

如何在Xamarin表单中为具有下划线效果的标签加下划线?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我按照教程创建下划线效果。然而,当我的页面开始时,它会毫无例外地被捕获。有人设法创造下划线效果吗?下面是一个代码: UnderlineEffect.cs: namespace XX.CustomForms { 公共类下划线效果:RoutingEffect { public const string EffectNamespace=“XX.CustomForms”; public UnderlineEffect():基($“{EffectNamespace}.{nameof(UnderlineEf

我按照教程创建下划线效果。然而,当我的页面开始时,它会毫无例外地被捕获。有人设法创造下划线效果吗?下面是一个代码:

UnderlineEffect.cs:

namespace XX.CustomForms
{
公共类下划线效果:RoutingEffect
{
public const string EffectNamespace=“XX.CustomForms”;
public UnderlineEffect():基($“{EffectNamespace}.{nameof(UnderlineEffect)}”)
{
}
}
}
UnderlineLabel_Droid.cs:

[程序集:ResolutionGroupName(UnderlineEffect.EffectNamespace)]
[程序集:导出效果(typeof(UnderlineEffect)、nameof(UnderlineEffect))]
命名空间XX.Droid.Renderers
{
公共类UnderlineEffect:PlatformEffect
{
受保护的覆盖无效附加()
{
设置下划线(true);
}
受保护的覆盖无效OnDetached()
{
设置下划线(假);
}
受保护的覆盖无效OneElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs参数)
{
base.OnElementPropertyChanged(args);
如果(args.PropertyName==Label.TextProperty.PropertyName | | args.PropertyName==Label.FormattedTextProperty.PropertyName)
{
设置下划线(true);
}
}
私有void SetUnderline(布尔下划线)
{
尝试
{
var textView=(textView)控件;
如果(下划线)
{
textView.PaintFlags |=PaintFlags.UnderlineText;
}
其他的
{
textView.PaintFlags&=~PaintFlags.UnderlineText;
}
}
捕获(例外情况除外)
{
Console.WriteLine(“无法在标签下划线。错误:”,例如消息);
}
}
}
}
还有我的xaml:

    xmlns:custom="clr-namespace:XX.CustomForms;assembly=XX"

    <Label Text="Privacy Notice" HorizontalTextAlignment="Center" >
        <Label.Effects>
            <custom:UnderlineEffect />
        </Label.Effects>
    </Label>
xmlns:custom=“clr命名空间:XX.CustomForms;assembly=XX”

为了能够向标签添加下划线,我们创建了从标签继承的自定义渲染器

公共类CustomLabel:标签
{
公共静态只读BindableProperty IsUnderlinedProperty=BindableProperty.Create(“IsUnderlined”、typeof(bool)、typeof(CustomLabel)、false);
公共图书馆
{
获取{return(bool)GetValue(IsUnderlinedProperty);}
set{SetValue(IsUnderlinedProperty,value);}
}
}
在xaml页面中,您可以将其用作:


请注意,
s
是在xaml页面的根元素中声明的名称空间

现在,Android中的渲染器应该是这样的:

公共类CustomLabelRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(Control!=null&&Element!=null)
{
if(((CustomLabel)元素).is下划线)
{
Control.PaintFlags=PaintFlags.UnderlineText;
}
}
}
}

有些人在Xamarin中获得带下划线文本的长度是疯狂的。这里有一种不用千行自定义渲染器的方法。负利润的把戏来自于


Xamarin表单在
标签
s中添加了一个
文本装饰
属性。更新至Xamarin Forms 3.3.0+,只需设置:

C#

XAML


请注意,当带下划线的
标签出现在
列表视图中时,iOS上存在错误。看起来它已在3.5.0中修复并发布。在我准备更新到最新版本之前,我现在仍然在iOS上使用自定义渲染器


因此,如果尚未更新到XF 3.5.0,请继续使用iOS效果。

在Label类中使用TextEditions属性


你能分享你到目前为止所做的事情吗?特别是,你得到的例外是什么?我添加了代码。我没有例外。调用InitializeComponent()时应用程序中断;无法使用try/catch捕获异常。手机上只有一条消息“不幸的是,XX已经停止”。请仔细检查您的构建输出,我认为其中有某种异常信息。在您发布的代码中,我能想到的唯一一件事是在
OnElementPropertyChanged
方法中,某些属性在初始化和中断时为null。我尝试在所有地方放置断点,但没有停止。调试器转到InitializeComponent();但自定义渲染器的资源消耗是否大于效果?它最终会影响应用程序的性能吗ễ 这是两年前的事了,从那时起我就没有和Xamarin合作过。但是,如果您仍然希望实现这一点,那么看起来Xamarin直接添加了效果,而无需创建自定义渲染器。看看
<StackLayout HorizontalOptions="Start">
    <Label Text="Underlined Text" />
    <BoxView HeightRequest="1" BackgroundColor="Purple" HorizontalOptions="FillAndExpand" Margin="0,-7,0,0" />
</StackLayout>
Label label = new Label {
    TextDecorations = TextDecorations.Underline
}
<Label TextDecorations="Underline"/>