當主窗體加載時間過長,這時候基本都會想添加一個等待操作來響應用戶點擊,提高用戶體驗。下面我記錄兩個方法,一點拙見,僅供參考。
方法1:在App類中使用SplashScreen類。
protected override void OnStartup(StartupEventArgs e)
{// 顯示啟動畫面ShowSplashScreen();base.OnStartup(e);
}private void ShowSplashScreen()
{// 這里要注意的點就是aa.jpg 屬性的生成的操作要設置為資源(Resource).SplashScreen splash = new SplashScreen(/Images/aa.jpg);/** 第一個參數表示是否自動關閉* 第二個參數表示啟動畫面窗口是否會被置于頂層,即使有其他程序的窗口處于活動狀態,* 啟動畫面也會顯示在它們之上.* 如果設置為false,表示遵循正常的窗口焦點規則,如果用戶切換到其他程序,* 啟動畫面可能會被其他窗口遮擋.* 如果設置為true,保證用戶一直能夠看到它.*/splash.Show(true, true);// 這里執行一些數據初始化的代碼//Task.Run(() =>//{// // 模擬耗時操作// Thread.Sleep(1000);// // 在UI線程上關閉啟動畫面// Dispatcher.Invoke(() =>// {// // 這里會有一個淡淡漸漸消失的效果// splash.Close(TimeSpan.FromSeconds(2));// });//});
}
也可以直接設置圖片的屬性-生成的操作一欄為SplashScreen。這樣就不用寫上面的代碼。
方法1缺點:僅能展示圖片,gif圖片也僅展示第一幀,如有辦法展示完整gif請給個傳送門。
方法2:自定義窗體作為歡迎屏幕
參考窗體代碼(后臺無操作,故僅貼前臺代碼,帶加載動畫的哦):我命名為:Splash
<Window x:Class="SmartHome.Splash"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" mc:Ignorable="d"Title="Splash" Width="640" Height="480" Background="#FF1D2128" ResizeMode="NoResize"ShowInTaskbar="False"SnapsToDevicePixels="True"TextOptions.TextFormattingMode="Display"UseLayoutRounding="True"WindowStartupLocation="CenterScreen"WindowStyle="None" Foreground="{x:Null}"><Grid><Rectangle HorizontalAlignment="Left" Height="60" Margin="173,117,0,0" Stroke="Black" VerticalAlignment="Top" Width="60" Fill="#FF2573DC" RadiusX="10" RadiusY="10"/><Rectangle Fill="#FF1D2128" HorizontalAlignment="Left" Height="43" Margin="185.332,125.671,0,0" RadiusY="5" RadiusX="5" Stroke="#FF1D2128" VerticalAlignment="Top" Width="10"/><Rectangle Fill="#FF1D2128" HorizontalAlignment="Left" Height="27.046" Margin="210.749,141.625,0,0" RadiusY="5" RadiusX="5" Stroke="#FF1D2128" VerticalAlignment="Top" Width="10"/><Rectangle Fill="#FF1D2128" HorizontalAlignment="Left" Height="10" Margin="189.25,141.625,0,0" RadiusY="5" RadiusX="5" Stroke="#FF1D2128" VerticalAlignment="Top" Width="31.499"/><Ellipse Fill="#FF1D2128" HorizontalAlignment="Left" Height="11" Margin="209.749,125.671,0,0" Stroke="#FF1D2128" VerticalAlignment="Top" Width="11"/><Path Data="M244,102 L244,187.2115" Fill="#FF2573DC" HorizontalAlignment="Left" Height="60" Margin="250,117,0,0" Stretch="Fill" Stroke="#FF2573DC" VerticalAlignment="Top" Width="3" StrokeThickness="3"/><Label Content="HAHAHAHA" Height="60" Margin="259,115,169,0" VerticalAlignment="Top" Foreground="#FF2573DC" FontSize="48" FontFamily="French Script MT" FontWeight="Bold"/><Path Data="M1.5000012,1.5 L14.500009,14.500008 M14.500005,12.39 L1.5,25.390005" Fill="#FF2573DC" HorizontalAlignment="Left" Margin="205.167,230.25,0,222.86" Stretch="Fill" Stroke="#FF2573DC" StrokeThickness="3" Width="16"/><Label Content="程序名稱" Margin="230.749,221,204,222.86" FontSize="24" Foreground="White"/><Label Content="即將進入" Margin="278.749,0,277,182.86" FontSize="16" Foreground="White" Height="36.14" HorizontalAlignment="Center" VerticalAlignment="Bottom"/><Grid Background="#FF1D2128" HorizontalAlignment="Left" Margin="249,301,0,99"><Grid.Resources><Style x:Key="rec" TargetType="Rectangle"><Setter Property="Width" Value="10"/><Setter Property="Height" Value="30"/><Setter Property="Fill" Value="#FF2573DC"/></Style><PowerEase x:Key="powerEase" Power="3" EasingMode="EaseInOut"/></Grid.Resources><Grid.Triggers><EventTrigger RoutedEvent="Loaded"><BeginStoryboard><Storyboard RepeatBehavior="Forever" Storyboard.TargetProperty="Height"><DoubleAnimation Storyboard.TargetName="rec1" To="50" BeginTime="0:0:0.0" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec2" To="50" BeginTime="0:0:0.1" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec3" To="50" BeginTime="0:0:0.2" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec4" To="50" BeginTime="0:0:0.3" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec5" To="50" BeginTime="0:0:0.4" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec6" To="50" BeginTime="0:0:0.5" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec7" To="50" BeginTime="0:0:0.6" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec8" To="50" BeginTime="0:0:0.7" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec9" To="50" BeginTime="0:0:0.8" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/><DoubleAnimation Storyboard.TargetName="rec10" To="50" BeginTime="0:0:0.9" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/></Storyboard></BeginStoryboard></EventTrigger></Grid.Triggers><Grid.ColumnDefinitions><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/><ColumnDefinition Width="15"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Label Content="Loading..."Grid.Row="1"FontSize="18"FontFamily="Times New Roman"FontWeight="Bold"Grid.ColumnSpan="10"VerticalContentAlignment="Center"HorizontalContentAlignment="Center"/><Rectangle Name="rec1" Grid.Column="0" Style="{StaticResource rec}"/><Rectangle Name="rec2" Grid.Column="1" Style="{StaticResource rec}"/><Rectangle Name="rec3" Grid.Column="2" Style="{StaticResource rec}"/><Rectangle Name="rec4" Grid.Column="3" Style="{StaticResource rec}"/><Rectangle Name="rec5" Grid.Column="4" Style="{StaticResource rec}"/><Rectangle Name="rec6" Grid.Column="5" Style="{StaticResource rec}"/><Rectangle Name="rec7" Grid.Column="6" Style="{StaticResource rec}"/><Rectangle Name="rec8" Grid.Column="7" Style="{StaticResource rec}"/><Rectangle Name="rec9" Grid.Column="8" Style="{StaticResource rec}"/><Rectangle Name="rec10" Grid.Column="9" Style="{StaticResource rec}"/></Grid></Grid>
</Window>
使用方式:
在APP類下設置新的單線程打開窗體并賦值給全局變量方便關閉:
//定義一個靜態的全局歡迎窗口
public static Splash Splash = new Splash();
protected override void OnStartup(StartupEventArgs e)
{base.OnStartup(e);Thread t = new Thread(() =>{Splash splash = new Splash();//把實例賦值給全局變量Splash= splash;//用Show的話動畫效果不流暢splash.ShowDialog();});t.Name = "HelloWindow";//設置為單線程。一定要設置t.SetApartmentState(ApartmentState.STA);t.Start();
}
主窗體啟動成功后的關閉方法:
public MainWindow()
{InitializeComponent();
//耗時的操作,比如實例化控件,頁面,初始化數據等
//...
//窗口內容呈現完成后的方法,用load方法也可以看自己
this.ContentRendered += MainWindow_ContentRendered;
}private void MainWindow_ContentRendered(object sender, EventArgs e)
{if (App.Splash != null){//在該線程上關閉App.Splash.Dispatcher.Invoke((Action)(() => App.Splash.Close()));}
}
一點拙見,還請指正。各位大佬有更好的方法還請留個傳送門,或評論區展現一下風采!!!!
好記性不如爛筆頭................................................................................................