尘叶心繁

Xamarin.Forms Label

电脑版发表于:2022/4/23 18:00

Xamarin.Forms Label

简介与属性列表


标签用于显示单行文本元素以及文本的多行块。
(下面是相关常用属性列表)

字段与属性 描述
Text 获取或设置 Label 的文本。
FontSize 获取标签的字体大小。
TextColor 获取或设置 Label 的文本的 Color。
LineHeight 获取或设置显示文本时要应用于默认行高的乘数。
HorizontalOptions 获取或设置 LayoutOptions,它定义元素在布局周期中的布局方式。
LineBreakMode 获取或设置 Label 的 换行模式。
FontAttributes 获取一个值,该值指示标签的字体是粗体、斜体还是两者皆否。
TextDecorations 获取或设置应用于 Text 的文字装饰。
Style 包含触发器、资源库和行为的类,这些所含内容将完全或部分定义可视元素类的外观和行为。
Command 命令
CommandParameter 命令所需要的参数。
FormattedString 表示某些部分应用了一些特性的文本。

案例


创建一个Xamarin应用后,添加一个Labels页面,并在Labels.xaml中添加如下内容:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <ContentPage
  3. xmlns="http://xamarin.com/schemas/2014/forms"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  5. x:Class="LableApp.Labels">
  6. <ContentPage.Content>
  7. <StackLayout>
  8. <Label Text="Label:1 Welcom to Xamarin.Forms!"/>
  9. <Label Text="Label:2 Welcom to Xamarin.Forms!" FontSize="Large"/>
  10. <Label Text="Label:3 Welcom to Xamarin.Forms!" FontSize="Large" TextColor="DarkBlue"/>
  11. <Label Text="Label:4 Welcom to Xamarin.Forms!" FontSize="Large" LineHeight="15"/>
  12. <Label Text="Label:5 Welcom to Xamarin.Forms!" HorizontalOptions="Center"/>
  13. <Label Text="Label:6 Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!"
  14. HorizontalOptions="Center"
  15. LineBreakMode="WordWrap"
  16. />
  17. <Label Text="Label:7 Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!Welcom to Xamarin.Forms!"
  18. HorizontalOptions="Center"
  19. LineBreakMode="NoWrap"
  20. />
  21. <Label Text="Label:8 Welcom to Xamarin.Forms!"
  22. TextColor="Blue"
  23. FontAttributes="Italic"
  24. FontSize="24"
  25. TextDecorations="Underline"
  26. HorizontalOptions="Center"
  27. />
  28. <Label LineBreakMode="WordWrap">
  29. <Label.FormattedText>
  30. <FormattedString>
  31. <Span Text="Label-9 Red Bold" TextColor="Red" FontAttributes="Bold"/>
  32. <Span Text="Label-10 default" Style="{DynamicResource BoldStyle}"/>
  33. <Span Text="Label-11 default" FontAttributes="Italic" FontSize="Small"/>
  34. </FormattedString>
  35. </Label.FormattedText>
  36. </Label>
  37. <Label TextColor="Gray" FontSize="Medium" LineBreakMode="WordWrap">
  38. <Label.FormattedText>
  39. <FormattedString>
  40. <Span Text="Label-11: TNBLOG Channel"/>
  41. <Span FontAttributes="Italic"/>
  42. <Span Text="Click Here" TextDecorations="Underline" TextColor="Blue">
  43. <Span.GestureRecognizers>
  44. <TapGestureRecognizer Command="{Binding TapCommand}" CommandParameter="https://www.tnblog.net/"/>
  45. </Span.GestureRecognizers>
  46. </Span>
  47. </FormattedString>
  48. </Label.FormattedText>
  49. </Label>
  50. </StackLayout>
  51. </ContentPage.Content>
  52. </ContentPage>


并且在后台呢,我们添加TapCommand命令方法。

  1. public partial class Labels : ContentPage
  2. {
  3. public ICommand TapCommand => new Command<string>(OpenBrowser);
  4. public Labels ()
  5. {
  6. InitializeComponent ();
  7. BindingContext = this;
  8. }
  9. void OpenBrowser(string url)
  10. {
  11. Launcher.OpenAsync(url);
  12. }
  13. }


改改App.xaml.cs的入口方法。

  1. public App ()
  2. {
  3. InitializeComponent();
  4. //MainPage = new MainPage();
  5. MainPage = new Labels();
  6. }

运行测试


首先我们通过安卓打开我们的APP,然后我们通过点击Tnblog链接跳转到Tnblog官网


接下来展示IOS的运行结果


我们发现这个中间的间距很大,我认为是每行它都是有一定的高度的,所以我们在第四个label的时候把LineHeight="15"去掉就可以了


接下来我将对上面比较有意义的属性设置的代码进行讲解。

静态资源与动态资源(StaticResource and DynamicResource)


静态资源(StaticResource)指的是:在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了,即使资源对象被改变也不会生效。
动态资源(DynamicResource)使用指的是:在程序运行过程中然会去访问资源,当资源对象被改变时,再次访问会访问到改变后的对象。
下面我们将改变一些代码来进行演示。

Demo


首先我们通过ContentPage.Resources定义两个Label样式(Style)资源对象Res1Res2,并且将它们最先开始都设置为绿色(Green)颜色。我们通过x:Key来定义资源的名称,通过TargetType来设置该资源指定用在哪个类型上,Setter来设置该类型要修改的属性(Property)与需要设置的属性值(Value),在Label下可以添加多个Setter来指定要修改该类型上的多个属性值。

  1. <ContentPage.Resources>
  2. <Color x:Key="FirstLabelColor">Green</Color>
  3. <Style x:Key="Res1" TargetType="{x:Type Label}">
  4. <Setter Property="BackgroundColor"
  5. Value="{StaticResource FirstLabelColor}" />
  6. </Style>
  7. <Style x:Key="Res2" TargetType="{x:Type Label}">
  8. <Setter Property="BackgroundColor"
  9. Value="{StaticResource FirstLabelColor}" />
  10. </Style>
  11. </ContentPage.Resources>


为了演示效果更明显,我们将Res1样式静态资源定义到Label1上,将Res2样式动态资源定义到Label2上,并且我们为Label2绑定点击命令(StyleCommand)。当我们点击Label2时,设置所有自定义资源都将改变为红色。

  1. <Label Text="Label:1 Welcom to Xamarin.Forms!" Style="{StaticResource Res1}" LineHeight="10"/>
  2. <Label Text="Label:2 Welcom to Xamarin.Forms!" Style="{DynamicResource Res2}" FontSize="Large">
  3. <Label.GestureRecognizers>
  4. <TapGestureRecognizer Command="{Binding StyleCommand}"/>
  5. </Label.GestureRecognizers>
  6. </Label>


关于这个StyleCommand命令我们对应的是StyleChange方法,我们发现有定义Color,Style,Setters….这些是从哪儿来的呢?其实就是从xaml上抄过来的,我们定义资源的时候定义了Style、Setter、以及它们的类型属性Label、Property、Value什么的。关于this.Resources这里是我们所有的资源对象集合。
这个BackgroundColorProperty是从Label上来的,也就是Label.BackgroundColorProperty

  1. public partial class Labels : ContentPage
  2. {
  3. public ICommand TapCommand => new Command<string>(OpenBrowser);
  4. public ICommand StyleCommand => new Command(StyleChange);
  5. void StyleChange()
  6. {
  7. Color color = Color.Red;
  8. var style = new Style(typeof(Label));
  9. style.Setters.Add(new Setter() { Property = BackgroundColorProperty, Value = color });
  10. this.Resources["Res1"] = style;
  11. this.Resources["Res2"] = style;
  12. }
  13. public Labels ()
  14. {
  15. InitializeComponent ();
  16. BindingContext = this;
  17. }
  18. void OpenBrowser(string url)
  19. {
  20. Launcher.OpenAsync(url);
  21. }
  22. }


好,接下来我们开始演示。
首先都是绿色,然后点击Label2后只有Label2变红了。


好,这是静态资源与动态资源,接下来我们来看看TapGestureRecognizer

TapGestureRecognizer (点击事件)


几乎在每一个标签都有点击事件,我们可以通过<标签><标签.GestureRecognizers><TapGestureRecognizer>来进行定义,后面结束定义就不写了。除了单击,我们还可以双击、三击、四击、无数击….等,这将取决NumberOfTapsRequired属性,当该属性设置为2就是双击,3就是三击以此类推。
Command就是后台需要触发的事件。
CommandParameter需要传的参数。如果要传参数的话,需要在后台的Command进行泛型实例化。

  1. <Span.GestureRecognizers>
  2. <TapGestureRecognizer Command="{Binding TapCommand}" CommandParameter="https://www.tnblog.net/"/>
  3. </Span.GestureRecognizers>
  1. public ICommand TapCommand => new Command<string>(OpenBrowser);
  2. void OpenBrowser(string url)
  3. {
  4. Launcher.OpenAsync(url);
  5. }
关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
猜你喜欢
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    / 9