【问题标题】:Xamarin Forms convert Xaml to common resource style Yummy PancakeViewXamarin Forms 将 Xaml 转换为通用资源样式 Yummy PancakeView
【发布时间】:2020-12-10 15:12:53
【问题描述】:

我正在尝试将我的 pancake 控件的属性转换为一种样式,以便我所有的 pancake 控件的样式都相同。这就是我的 XAML 的样子。

    <yummy:PancakeView x:Name="pvMEssage" BackgroundColor="{DynamicResource ExceptionBackground}" CornerRadius="0,0,20,20" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                       Padding="0,0,0,0" Margin="0,0,0,0">
        <yummy:PancakeView.BackgroundGradientStops StartPoint="0,0" EndPoint="0,0">
            <yummy:GradientStop Offset="0.1" Color="{DynamicResource ExceptionBackground}" />
        </yummy:PancakeView.BackgroundGradientStops>
        <yummy:PancakeView.Border>
            <yummy:Border Color="White" Thickness="2" />
        </yummy:PancakeView.Border>

我知道如何在样式中使用 setter.value,但我不知道如何将 backgroundgradientstops 放入样式中。

到目前为止我有这个:

            <Style x:Key="PancakeViewTop" TargetType="yummy:PancakeView">
            <Setter Property="CornerRadius" Value="20,20,0,0" />
            <Setter Property="HorizontalOptions" Value="FillAndExpand" />
            <Setter Property="VerticalOptions" Value="FillAndExpand" />
            <Setter Property="Padding" Value="0,0,0,0" />
            <Setter Property="Margin" Value="0,0,0,0" />
            <Setter Property="BackgroundGradientStartPoint" Value="0,0" />
            <Setter Property="BackgroundGradientEndPoint" Value="0,0" />
            <Setter Property="BackgroundGradientStops">
                <Setter.Value>
                    <yummy:GradientStop Offset="0.1" Color="{DynamicResource ExceptionBackground}" />
                </Setter.Value>
            </Setter>
            <Setter Property="Border">
                <Setter.Value>
                    <yummy:Border Color="White" Thickness="2" />
                </Setter.Value>
            </Setter>
        </Style>

但我在这些行中遇到错误

<Setter Property="BackgroundGradientStops">
    <Setter.Value>

表示“无法分配指定的值。应为以下类型:“GradientStopCollection”

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

    尝试:

    <Setter Property="BackgroundGradientStops">
        <Setter.Value>
            <pancake:GradientStopCollection>
                <pancake:GradientStop Offset="0.1" Color="{DynamicResource ExceptionBackground}"/>
                <pancake:GradientStop Offset="0.5" Color="{DynamicResource OtherColor}"/>
            </pancake:GradientStopCollection>
        </Setter.Value>
    </Setter>
    

    另外你需要指定至少 2 种颜色 (2 GradientStop) 否则会抛出异常

    Java.Lang.IllegalArgumentException: '需要 >= 2 种颜色'

    只有一种颜色的渐变是没有意义的。

    【讨论】:

    • 好吧,做一种颜色确实有意义我这样做只是为了解决 UWP 上的煎饼控件中的一个缺陷,即背景颜色没有被裁剪到圆角的边界。这种解决方法记录在他们的 github 项目中的 pancake control 问题中。其余的我会尝试看看它是否有效并回发。谢谢。
    • 很好,您可以在评论中发布此问题的链接,也许对某人有用。
    • 这里是问题的链接并提供了解决方法:github.com/sthewissen/Xamarin.Forms.PancakeView/issues/150
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2020-05-19
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多