【问题标题】:Is it possible to give a Xamarin Frame different border colors?是否可以为 Xamarin Frame 提供不同的边框颜色?
【发布时间】:2018-11-27 04:37:50
【问题描述】:

这是我拥有的 XAML:

<Frame CornerRadius="1" HasShadow="false" Margin="10" 
   BackgroundColor="White" BorderColor="Silver" Padding="0" >

我在 iOS 上的 Google 翻译中看到他们使用类似这种框架的东西来围绕设置中的不同行。但是,它们在顶部和底部具有不同的边框颜色。

有谁知道有没有办法用框架做?

【问题讨论】:

  • 能否提供不同边框颜色的谷歌翻译截图?
  • 嗨,杰克,我已经做到了。这是问题的一部分。注意底部的颜色略有不同。
  • TableView 会给你这种开箱即用的外观,你试过了吗? docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
  • 这不是两种颜色。那是框架的底部阴影!

标签: xamarin xamarin.forms


【解决方案1】:

你可以用一个组件来实现,像这样

BorderEntryComponent.xaml

<?xml version="1.0" encoding="UTF-8"?>
<StackLayout
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="X.Y.Z.BorderEntryComponent"

    Spacing="0">
    <BoxView
        x:Name="TopBorder"
        HeightRequest="2"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="EndAndExpand" />
    <Entry x:Name="Entry" />
    <BoxView
        x:Name="BottomBorder"
        HeightRequest="2"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="EndAndExpand" />
</StackLayout>

而且,在你的BorderEntryComponent.xaml.cs

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Xamarin.Forms;

namespace X.Y.Z
{
    public partial class BorderEntryComponent : StackLayout
    {    
        public static readonly BindableProperty TopColorProperty =
            BindableProperty.Create(nameof(TopColor), typeof(Color), typeof(BorderEntryComponent), default(Color), BindingMode.OneWay);

        public static readonly BindableProperty BottomColorProperty =
            BindableProperty.Create(nameof(BottomColor), typeof(Color), typeof(BorderEntryComponent), default(Color), BindingMode.OneWay);

        public UnderlineEntryComponent()
        {
            InitializeComponent();
        }

        protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            base.OnPropertyChanged(propertyName);

            if (propertyName == TopColorProperty.PropertyName)
            {
                this.TopBorder.Color = TopColor;
            }
            else if (propertyName == BottomColorProperty.PropertyName)
            {
                this.BottomBorder.Color = BottomColor;
            }
        }

        public Color TopColor
        {
            get => (Color)GetValue(TopColorProperty);
            set => SetValue(TopColorProperty, value);
        }

        public Color BottomColor
        {
            get => (Color)GetValue(BottomColorProperty);
            set => SetValue(BottomColorProperty, value);
        }
    }
}

然后,您只需在 .xaml 上执行此操作

<components:UnderlineEntryComponent
                    TopColor = "Blue"
                    BottomColor = "Black" />

您可以阅读更多关于可绑定属性here

【讨论】:

    【解决方案2】:

    AFAIK,您没有针对您要查找的内容的内置选项。 您可以通过在彼此之上绘制具有不同颜色和属性的多个帧来玩玩,但这对我来说有点太“hacky”了。

    我建议您为自己的 Frame 控件创建一个Custom Render。这样一来,您就可以随心所欲地绘制框架,并在任何其他地方重复使用您的控件。

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      相关资源
      最近更新 更多