Uro*_*ros 6 xamarin xamarin.forms
我按照本教程创建了下划线效果.但是,当我的页面启动时,它会被捕获,无一例外.有没有人设法创建下划线效果?这是一个代码:
UnderlineEffect.cs:
namespace XX.CustomForms
{
public class UnderlineEffect : RoutingEffect
{
public const string EffectNamespace = "XX.CustomForms";
public UnderlineEffect() : base($"{EffectNamespace}.{nameof(UnderlineEffect)}")
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
UnderlineLabel_Droid.cs:
[assembly: ResolutionGroupName(UnderlineEffect.EffectNamespace)]
[assembly: ExportEffect(typeof(UnderlineEffect), nameof(UnderlineEffect))]
namespace XX.Droid.Renderers
{
public class UnderlineEffect : PlatformEffect
{
protected override void OnAttached()
{
SetUnderline(true);
}
protected override void OnDetached()
{
SetUnderline(false);
}
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);
if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
{
SetUnderline(true);
}
}
private void SetUnderline(bool underlined)
{
try
{
var textView = (TextView)Control;
if (underlined)
{
textView.PaintFlags |= PaintFlags.UnderlineText;
}
else
{
textView.PaintFlags &= ~PaintFlags.UnderlineText;
}
}
catch (Exception ex)
{
Console.WriteLine("Cannot underline Label. Error: ", ex.Message);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我的xaml:
xmlns:custom="clr-namespace:XX.CustomForms;assembly=XX"
<Label Text="Privacy Notice" HorizontalTextAlignment="Center" >
<Label.Effects>
<custom:UnderlineEffect />
</Label.Effects>
</Label>
Run Code Online (Sandbox Code Playgroud)
hva*_*an3 23
Xamarin Forms TextDecorations为Labels 添加了一个属性.更新到Xamarin Forms 3.3.0+并设置:
Label label = new Label {
TextDecorations = TextDecorations.Underline
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果带有下划线,Label则iOS上存在错误ListView.看起来它已经修复但尚未发布.我现在仍在iOS上使用自定义渲染器,直到修复为止.
因此,在发布此修复程序之前,请继续使用iOS效果.
use*_*234 10
有些人要在Xamarin中获得带下划线的文本的长度太疯狂了。这是一种无需一千行自定义渲染器的方法。负利润的trick俩来自这个家伙。
<StackLayout HorizontalOptions="Start">
<Label Text="Underlined Text" />
<BoxView HeightRequest="1" BackgroundColor="Purple" HorizontalOptions="FillAndExpand" Margin="0,-7,0,0" />
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
为了能够向标签添加下划线,我们创建了继承自 Label 的自定义渲染器。
public class CustomLabel : Label
{
public static readonly BindableProperty IsUnderlinedProperty = BindableProperty.Create("IsUnderlined", typeof(bool), typeof(CustomLabel), false);
public bool IsUnderlined
{
get { return (bool) GetValue(IsUnderlinedProperty); }
set { SetValue(IsUnderlinedProperty, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
在您的 xaml 页面中,您可以将其用作:
<s:CustomLabel IsUnderlined="True" Text="UnderlinedText" FontSize="18" HorizontalOptions="CenterAndExpand">
Run Code Online (Sandbox Code Playgroud)
请注意,这s是在 xaml 页面的根元素中声明的命名空间。
现在 Android 中的渲染器将是这样的:
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null && Element != null)
{
if (((CustomLabel)Element).IsUnderlined)
{
Control.PaintFlags = PaintFlags.UnderlineText;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10114 次 |
| 最近记录: |