WPF窗口设置全屏显示,以及加载过程中卡顿

在使用C# WPF框架开发项目时,遇到窗口全屏显示时出现卡顿现象。经过研究发现,卡顿是由于在Window_Loaded方法中设置全屏导致的。解决方案是在SourceInitialized方法中设置全屏,或者直接在XAML文件的Window标签内配置全屏选项,从而避免界面加载时的卡顿问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在用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窗口卡顿的问题得以解决,在此也感谢这位博主的文章给我的帮助

故不积跬步,无以至千里,还是要多学习。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值