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

.NET MAUI中如何制作包含应用版本号的启动屏?

在.NET MAUI中实现带Logo与版本号的启动屏

完全可行,以下两种方案可以实现需求:

方案一:平台原生启动屏嵌入版本号

.NET MAUI的原生启动屏由各平台独立资源控制,需针对Android、iOS、Windows分别配置:

Android

  1. 编辑Platforms/Android/Resources/drawable/splash_screen.xml(无则新建),这是启动屏的布局文件,添加Logo和版本号文本:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/splash_background" />
  <!-- 居中显示Logo -->
  <item>
    <bitmap
      android:gravity="center"
      android:src="@mipmap/appicon" />
  </item>
  <!-- 底部显示版本号 -->
  <item>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="版本:@string/app_version"
      android:textColor="#FFFFFF"
      android:textSize="14sp"
      android:gravity="bottom|center"
      android:layout_marginBottom="32dp" />
  </item>
</layer-list>
  1. 在Platforms/Android/Resources/values/strings.xml中添加动态版本号引用:
<string name="app_version">${versionName}</string>
  1. 确保Platforms/Android/build.gradle中配置了版本信息:
android {
    defaultConfig {
        versionCode 1
        versionName "1.0.0"
        // 其他配置项
    }
}

iOS

  1. 打开Platforms/iOS/Resources/LaunchScreen.storyboard。
  2. 拖入ImageView放置Logo,调整到合适位置;再拖入Label控件到页面底部。
  3. 选中Label,在Attributes Inspector的Text字段输入Version: ,点击右侧Insert Variable按钮,选择CFBundleShortVersionString(版本名)或CFBundleVersion(内部版本号)。

Windows

  1. 打开Platforms/Windows/Package.appxmanifest,切换到Visual Assets标签。
  2. 找到Splash Screen区域,点击Edit启动编辑器。
  3. 在编辑器中添加文本元素,输入Version: {{{Version}}},系统会自动关联应用版本;同时添加Logo图片并调整布局。

方案二:跨平台自定义启动页(模拟启动屏)

如果想要统一跨平台实现,可创建一个模拟启动屏的页面,在应用初始化后显示并自动跳转:

  1. 创建SplashPage.xaml:
<?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="YourAppNamespace.SplashPage"
             BackgroundColor="#2196F3">
    <VerticalStackLayout HorizontalOptions="Center" VerticalOptions="Center">
        <Image Source="appicon.png" WidthRequest="120" HeightRequest="120" />
        <Label x:Name="VersionLabel" 
               TextColor="White" 
               FontSize="14"
               Margin="0,20,0,0" />
    </VerticalStackLayout>
</ContentPage>
  1. 编辑SplashPage.xaml.cs,获取版本号并添加跳转逻辑:
using Microsoft.Maui.ApplicationModel;

namespace YourAppNamespace;

public partial class SplashPage : ContentPage
{
    public SplashPage()
    {
        InitializeComponent();
        LoadVersion();
        _ = NavigateToMain();
    }

    private void LoadVersion()
    {
        VersionLabel.Text = $"版本 {AppInfo.VersionString}";
    }

    private async Task NavigateToMain()
    {
        await Task.Delay(2000); // 自定义启动延迟时长
        await Shell.Current.GoToAsync("//MainPage");
    }
}
  1. 修改App.xaml.cs,将启动页设为SplashPage(若使用Shell需先注册路由):
public App()
{
    InitializeComponent();
    MainPage = new AppShell();
    // 注册路由(若使用Shell)
    Routing.RegisterRoute(nameof(SplashPage), typeof(SplashPage));
    // 启动时导航到启动页
    _ = Shell.Current.GoToAsync(nameof(SplashPage));
}

注意:此方案的启动屏在应用初始化完成后显示,若应用启动较慢,可结合原生启动屏使用,让两者样式一致实现无缝过渡。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 00:40:17