.NET MAUI 应用程序中的渐变页面背景

Sam*_*Sam 6 xaml maui

如何在 .NET MAUI 应用程序中将页面设置为具有线性背景?

我尝试定义LinearGradientBrushin Colors.xaml,然后将其分配为StaticResource- 见下文 - 但这似乎不起作用。

在 中Colors.xaml,我有这个:

<LinearGradientBrush x:Key="PageGradientBackground" EndPoint="1,1">
     <GradientStop
         Color="{StaticResource UIOffWhite}"
         Offset="0.1"/>
     <GradientStop
         Color="{StaticResource UILightGray}"
         Offset="0.6"/>
     <GradientStop
         Color="{StaticResource UIMidGray}"
         Offset="1.0"/>
</LinearGradientBrush>
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             BackgroundColor="{StaticResource PageGradientBackground}">
    <!-- ... -->
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

我也尝试过内联定义它,但这也不起作用。这实际上是不允许的:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentPage.BackgroundColor>
        <LinearGradientBrush>
        </LinearGradientBrush>
    </ContentPage.BackgroundColor>
    <!-- ... -->
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

知道如何为 a 提供渐变背景ContentPage吗?

Gua*_*SFT 8

您可以使用LinearGradientBrush类似下面的代码。您可以将其放入LinearGradientBrush<ContentPage.Background>它可以很好地工作。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp7.MainPage">
    <ContentPage.Background>
        
            <LinearGradientBrush EndPoint="1,0">
                <GradientStop Color="Yellow"
                          Offset="0.1" />
                <GradientStop Color="Green"
                          Offset="1.0" />
            </LinearGradientBrush>

    </ContentPage.Background>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

更多信息,您可以参考这篇文章线性渐变画笔