<Window x:Class="LoadingScreenDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LoadingScreenDemo"
mc:Ignorable="d"
WindowStyle="None"
AllowsTransparency="True"
WindowState="Maximized"
Background="Transparent"
Loaded="Window_Loaded">
<Border Margin="20" CornerRadius="10">
<Grid>
<Grid x:Name="LoadingOverlay"
Background="#FF0A0A0A"
Opacity="1"
Panel.ZIndex="100"
IsHitTestVisible="True">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center">
<ProgressBar x:Name="LoadingProgress"
Width="400"
Height="20"
Margin="0,0,0,20"
IsIndeterminate="True"
Foreground="#FF4A90E2"
Style="{StaticResource ModernProgressBar}"/>
<TextBlock Text="正在初始化应用程序..."
HorizontalAlignment="Center"
Foreground="#CCFFFFFF"
FontSize="18"
FontWeight="SemiBold"/>
<TextBlock Text="请稍候,正在准备您的体验"
HorizontalAlignment="Center"
Margin="0,10,0,0"
Foreground="#99FFFFFF"
FontSize="14"/>
StackPanel>
Grid>
Grid>
<Grid x:Name="MainContent" Opacity="0" Panel.ZIndex="0">
<Grid.Background>
<ImageBrush ImageSource="https://images.unsplash.com/photo-1519681393784-d120267933ba?ixlib=rb-4.0.3&auto=format&fit=crop&w=1920&q=80"
Stretch="UniformToFill"
Opacity="0.9"/>
Grid.Background>
<Rectangle Fill="#80111111"/>
<Grid Margin="30">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
Grid.RowDefinitions>
<Border Grid.Row="1" Background="#20000000" Padding="20">
Border>
Grid>
Border>
Window>
using System.Windows;
using System.Windows.Media.Animation;
using System.Threading.Tasks;
namespace LoadingScreenDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += Window_Loaded;
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
// 模拟应用程序初始化过程
await SimulateLoadingAsync();
// 加载完成后显示主界面
ShowMainContent();
}
private async Task SimulateLoadingAsync()
{
// 模拟加载进度
for (int i = 0; i < 100; i++)
{
await Task.Delay(30); // 模拟耗时操作
// 更新进度(实际应用中替换为真实进度)
if (i % 10 == 0)
{
// 更新加载提示文本
if (i < 30)
LoadingProgress.ToolTip = "正在初始化核心模块...";
else if (i < 60)
LoadingProgress.ToolTip = "正在加载用户数据...";
else if (i < 90)
LoadingProgress.ToolTip = "正在准备用户界面...";
else
LoadingProgress.ToolTip = "完成初始化...";
}
}
}
private void ShowMainContent()
{
// 创建动画
var fadeInAnimation = new DoubleAnimation
{
From = 0,
To = 1,
Duration = TimeSpan.FromSeconds(1.5),
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }
};
var fadeOutAnimation = new DoubleAnimation
{
From = 1,
To = 0,
Duration = TimeSpan.FromSeconds(1),
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }
};
// 动画完成后移除加载层
fadeOutAnimation.Completed += (s, args) =>
{
LoadingOverlay.Visibility = Visibility.Collapsed;
LoadingOverlay.IsHitTestVisible = false; // 允许与主内容交互
};
// 启动动画
MainContent.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
LoadingOverlay.BeginAnimation(UIElement.OpacityProperty, fadeOutAnimation);
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = this.WindowState == WindowState.Maximized
? WindowState.Normal
: WindowState.Maximized;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
LoadingOverlay
网格覆盖整个窗口Panel.ZIndex="100"
确保加载层在最上方IsHitTestVisible="True"
阻止用户与下层内容交互ProgressBar
显示加载进度(设置为不确定模式)DoubleAnimation
创建透明度渐变动画CubicEase
缓动函数实现平滑过渡LoadingOverlay.IsHitTestVisible="True"
捕获所有输入事件IsHitTestVisible="False"
恢复交互private async Task RealLoadingAsync()
{
// 1. 初始化核心模块
await InitializeCoreModules();
UpdateLoadingText("核心模块初始化完成...");
// 2. 加载配置数据
await LoadConfiguration();
UpdateLoadingText("配置加载完成...");
// 3. 连接数据库/服务
await ConnectToServices();
UpdateLoadingText("服务连接成功...");
// 4. 准备用户界面
await PrepareUserInterface();
UpdateLoadingText("界面准备就绪...");
}
private void UpdateLoadingText(string message)
{
Dispatcher.Invoke(() => {
LoadingProgress.ToolTip = message;
});
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
await RealLoadingAsync();
ShowMainContent();
}
catch (Exception ex)
{
// 显示错误信息
ShowErrorDialog("初始化失败", ex.Message);
// 退出应用
Application.Current.Shutdown();
}
}
这个实现提供了专业的应用启动体验,确保在加载过程中用户无法与主界面交互,加载完成后通过平滑过渡显示主界面内容。您可以根据实际需求调整加载时间、动画效果和界面设计。