最近在用C# 的WPF框架做一个项目,其中window页面显示时需要全屏显示。
由于之前对这块不是很熟悉,就网上百度了一些WPF下如何将窗口全屏显示,得到的结果差不多是这样,
this.Topmost = true;
this.Left = 0.0;
this.Top = 0.0;
this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.WindowState = System.Windows.WindowState.Normal;
this.WindowStyle = System.Windows.WindowStyle.None;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
于是我把上面代码放在Window_Loaded方法中,
private void FourStageIntroWindow_Loaded(object sender, RoutedEventArgs e)
{
Console.WriteLine("**********************Loaded***************");
this.Topmost = true;
this.Left = 0.0;
this.Top = 0.0;
this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.WindowState = System.Windows.WindowState.Normal;
this.WindowStyle = System.Windows.WindowStyle.None;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
}
运行后发现我的Window页面终于可以全屏显示了,但是有一个问题,全屏显示的时候它是一顿一顿的,不是直接上来全屏显示在屏幕上。
为了解决这个问题,于是我就各种百度,后来发现是我设置全屏显示代码的时机不对,我在loaded方法里设置全屏显示时界面已经加载完成了,此时你再去设置全屏卡顿是必然啦。
解决步骤
接下来我也对WPF窗体的生命周期进行了测试,在window()方法中注册好几个生命周期方法
public FourStageIntroWindow()
{
InitializeComponent();
this.Loaded += FourStageIntroWindow_Loaded;
this.Activated += FourStageIntroWindow_Activated;
this.ContentRendered += FourStageIntroWindow_ContentRendered;
this.Deactivated += FourStageIntroWindow_Deactivated;
this.Unloaded += FourStageIntroWindow_Unloaded;
this.SourceInitialized += FourStageIntroWindow_SourceInitialized;
this.Closed += FourStageIntroWindow_Closed;
this.Closing += FourStageIntroWindow_Closing;
}
同时并在各个方法里打印下日志
private void FourStageIntroWindow_Unloaded(object sender, RoutedEventArgs e)
{
Console.WriteLine("**********************Unloaded***************");
}
private void FourStageIntroWindow_Deactivated(object sender, EventArgs e)
{
Console.WriteLine("**********************Deactivated***************");
}
private void FourStageIntroWindow_ContentRendered(object sender, EventArgs e)
{
Console.WriteLine("**********************ContentRendered***************");
}
private void FourStageIntroWindow_Activated(object sender, EventArgs e)
{
Console.WriteLine("**********************Activated***************");
}
private void FourStageIntroWindow_Loaded(object sender, RoutedEventArgs e)
{
Console.WriteLine("**********************Loaded***************");
}
private void FourStageIntroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Console.WriteLine("**********************Closing***************");
}
private void FourStageIntroWindow_Closed(object sender, EventArgs e)
{
Console.WriteLine("**********************Closed***************");
}
private void FourStageIntroWindow_SourceInitialized(object sender, EventArgs e)
{
Console.WriteLine("**********************SourceInitialized***************");
}
经运行程序,验证如下:
由此可见WPF窗口的Loaded方法前面还两个生命周期函数,此时为了让界面加载显示的时候不卡顿,
1.我们就把全屏显示设置方法等在SourceInitialized方法中,因为窗口初始化的时候会直接调用SourceInitialized 方法
private void FourStageIntroWindow_SourceInitialized(object sender, EventArgs e)
{
Console.WriteLine("**********************SourceInitialized***************");
this.Topmost = true;
this.Left = 0.0;
this.Top = 0.0;
this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.WindowState = System.Windows.WindowState.Normal;
this.WindowStyle = System.Windows.WindowStyle.None;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
}
2.直接来到XMAL文件中在Window标签里设置 全屏方法也是可以的
<Window x:Class="SPPB_Assessment.FourStageIntroWindow"
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:SPPB_Assessment"
mc:Ignorable="d"
Topmost="True" Left="0" Top="0" WindowState="Normal" ResizeMode ="NoResize"
WindowStyle="None"
Title="" Height="1920" Width="1080" Background="#05655B">
<Grid>
</Grid>
</Window>
至此,全屏显示wpf窗口卡顿的问题得以解决,在此也感谢这位博主的文章给我的帮助
故不积跬步,无以至千里,还是要多学习。