前言
随着项目越来越复杂,我们可能会在App中添加很多逻辑,这些逻辑可能还要保证在窗口加载前执行,这样就导致在配置相对较差的电脑上,用户看到第一个界面的时间就会较长,体验不好,怎样解决呢。
我们可以添加一个启动页面,这个启动页在显示后先处理之前App中的逻辑,处理完后再加载原来的第一个页面,比如是登录页。
欢迎页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <Window x:Class="SchoolClient.Wins.WelcomeWin" 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" Name="欢迎页面" Title="欢迎页面" Width="300" Height="80" AllowsTransparency="True" Background="Transparent" ContentRendered="WelcomeRndered" ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStartupLocation="CenterScreen" WindowState="Normal" WindowStyle="None" mc:Ignorable="d"> <Border Background="#f5f7f9" BorderBrush="#ddd" BorderThickness="1"> <Grid> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="26" Foreground="#666666" Text="应用启动中..." /> </Grid> </Border> </Window>
|
注意这里使用的是ContentRendered事件,因为这个事件会在页面显示后触发。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13
| private bool _isRun;
private void WelcomeRndered(object sender, EventArgs e) { if (!_isRun) { _isRun = true; MyApp.DelayRun(); MainWindow mainWindow = new MainWindow(); mainWindow.Show(); Close(); } }
|
注意
为了方式方法被触发多次,这里使用变量进行判断。
之前的App加载时的方法都放在了DelayRun中。
设置欢迎页为启动页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <Application x:Class="SchoolClient.MyApp" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:zConverter="clr-namespace:ZConverter" StartupUri="Wins/WelcomeWin.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/Resources/OverwrideDefaultControlStyles.xaml" /> </ResourceDictionary.MergedDictionaries> <zConverter:TongjiBgConverter x:Key="BgConv" /> </ResourceDictionary> </Application.Resources> </Application>
|