【问题标题】:.NET MAUI style problem with gradient colors - Android.NET MAUI 风格的渐变颜色问题 - Android
【发布时间】:2022-11-24 14:06:04
【问题描述】:

我最近开始使用 .NET MAUI,我发现了样式问题。 我从基本项目开始,以确保问题不是我在此过程中造成的。 该项目以一个按钮开始,每次用户按下该按钮时都会更改文本和大小。

问题是任何时候我直接或通过全局样式使用 LinearGradientBrush,按钮大小都不会改变以适合文本,更糟糕的是它会向左移动。

我到处看,但没有找到任何类似的问题或解决方案。

它适用于 Windows 但不适用于 Android

代码

<Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#8A26ED"/>
                        <GradientStop Color="#381061" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>

【问题讨论】:

  • 我确认在将 &lt;Button.Background&gt;... xaml 添加到标准模板时会发生这种情况。请在 github maui issues 创建一个问题。

标签: maui maui-community-toolkit


【解决方案1】:

我可以重现你的问题,这真的很奇怪。我的解决方法是在按钮单击事件处理程序中再次设置背景:

private void OnCounterClicked(object sender, EventArgs e)
{
    LinearGradientBrush ll = new LinearGradientBrush();
    ll.EndPoint = new Point(0, 1);
    GradientStop a = new GradientStop(Color.FromHex("#8A26ED"), 0);
    GradientStop b = new GradientStop(Color.FromHex("#381061"), 1);
    ll.GradientStops = new GradientStopCollection()
    {
        a,b
    };
    CounterBtn.Background = ll;  
    ...
}

这对我行得通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2021-11-18
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    相关资源
    最近更新 更多