You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为MAUI框架的MauiSplashScreen添加渐变背景?

MauiSplashScreen 渐变背景实现方案

MauiSplashScreen 的 .csproj 配置确实不支持直接设置渐变背景,你可以通过以下两种方案实现需求:

方案一:生成带渐变背景的静态资源(跨平台通用,最简单)

用设计工具(Figma/Photoshop/Canva等)创建包含渐变背景+你的SVG前景的图片(保存为PNG或SVG),直接将这个图片作为启动屏源文件配置:

修改项目 .csproj 文件中的 MauiSplashScreen 节点:

<MauiSplashScreen Include="Resources\Images\splash_with_gradient.png" Resize="false" />

这种方式无需修改平台代码,各平台编译时会直接使用带渐变的完整图片,效果一致。

方案二:分平台自定义原生启动屏(更灵活)

MAUI 的启动屏本质是各平台原生组件,可通过修改各平台的原生配置实现渐变背景:

Android 平台

  1. 在 Platforms/Android/Resources/drawable 目录下创建 splash_screen.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 渐变背景层 -->
    <item>
        <shape>
            <gradient
                android:startColor="#808080"
                android:endColor="#000080"
                android:angle="45" /> <!-- 角度控制渐变方向 -->
        </shape>
    </item>
    <!-- 前景图标层 -->
    <item>
        <bitmap
            android:src="@drawable/hikari_white_rings"
            android:gravity="center" />
    </item>
</layer-list>
  1. 修改 Platforms/Android/Resources/values/styles.xml 中的启动屏样式:
<style name="MainTheme.Splash" parent="Theme.Maui.SplashScreen">
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>
  1. 在 Platforms/Android/AndroidManifest.xml 中确保启动 Activity 使用该样式:
<activity
    android:name=".MainActivity"
    android:exported="true"
    android:theme="@style/MainTheme.Splash">

iOS 平台

  1. 打开 Platforms/iOS/Resources/LaunchScreen.storyboard
  2. 在可视化编辑器中:
    • 添加一个 Gradient View 作为根视图的背景,设置渐变颜色为 #808080 到 #000080,调整方向
    • 添加一个 ImageView,设置图片为 hikari_white_rings,并设置居中对齐
  3. 保存后,iOS 会自动使用这个故事板作为启动屏

Windows 平台

  1. 在 Platforms/Windows 目录下创建 SplashScreen.xaml 文件:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Background="Transparent">
    <Grid.Background>
        <LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
            <GradientStop Color="#808080" Offset="0"/>
            <GradientStop Color="#000080" Offset="1"/>
        </LinearGradientBrush>
    </Grid.Background>
    <Image Source="ms-appx:///Resources/Images/hikari_white_rings.svg"
           HorizontalAlignment="Center"
           VerticalAlignment="Center"/>
</Grid>
  1. 修改 Platforms/Windows/Package.appxmanifest 中的启动屏配置:
<uap:SplashScreen Image="SplashScreen.xaml" />

内容的提问来源于stack exchange,提问作者LordAhune

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 23:55:28