【问题标题】:How to add a border to your Xamarin Form Label?如何为 Xamarin 表单标签添加边框?
【发布时间】:2017-06-28 19:03:59
【问题描述】:

您好 Xamarin Forms 用户,

我已经发现 Xamarin Froms 不支持开箱即用的标签边框。所以经过一番搜索后仍然不知道如何使它成为可能。是否可以使用自定义渲染器添加边框?如果是这样,有人有例子吗?如果没有,是否有人有任何其他开箱即用的想法来实现这一点。

提前谢谢你

【问题讨论】:

  • 将标签添加到框架

标签: c# xamarin.forms label border custom-renderer


【解决方案1】:

您可以在Frame 元素中添加Label,并为Frame 设置OutlineColor

<Frame OutlineColor="Black">
    <Label Text="My Label With Frame" />
</Frame>

如果您想使用自定义渲染器,您应该为您想要支持的每个平台(即 Android、iOS、UWP、WinPhone)实现自定义渲染器

【讨论】:

  • 感谢您的快速回答,但这并不是我真正想要的。我提出的解决方案是使用 boxview 在我的标签下实例化它,高度为 1。我将在下面给出答案,向您展示我做了什么。
  • 简短说明:Frame.OutlineColor 在 Xamarin Forms 3 中已过时。它被属性 Frame.BorderColor 替换。
【解决方案2】:

我想出了一些开箱即用的想法,并想出了使用 boxview 作为边框。 这是我编写的代码示例:

  <StackLayout x:Name="BasicInfo" Margin="10,10,10,5" Grid.Row="0" Grid.Column="0">
    <Label Text="Basic Info" FontSize="20"/>
    <BoxView Color="Black" WidthRequest ="100" HeightRequest="1"/>
     <Label x:Name="text1" />
     <Label x:Name="text2"/>
     <Label x:Name="text3"/>
     <Label x:Name="text4"/>  
  </StackLayout>

我还会添加它给我的结果图片:

【讨论】:

    【解决方案3】:

    尽管已经有了答案,但我找到的解决方案允许您选择您特别想要显示的边框以及显示多少。

    我使用的修复方法是将需要边框的元素包装在 ContentView 中,为该 ContentView 提供 backgroundColour 和填充。代码如下

    声明了以下样式的资源字典

    <Style TargetType="ContentView"
           x:Key="BorderContentView">
        <Setter Property="BackgroundColor"
                Value="Black" />
        <Setter Property="Padding"
                Value="1 2 1 3" />
        <!-- Tweak the values above to set your borders however you prefer -->
    </Style>
    

    在您的视图中,只需添加一个包装 ContentView 并为其添加样式

    <ContentView Style="{DynamicResource BorderContentView}">
        <!-- Elements with a border here --> 
    </ContentView>
    

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 2014-06-30
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2011-04-06
      • 2020-05-11
      相关资源
      最近更新 更多