简体   繁体   English

UWP:如何获取任务栏高度

[英]UWP: How to get the TaskBar Height

I am making an UWP application.My requirement is to get the size of the TaskBar programatically (This application will run on Tablets of different resolution). 我正在制作一个UWP应用程序,我的要求是以编程方式获取TaskBar的大小(此应用程序将在不同分辨率的平板电脑上运行)。 After following many answers on stackoverflow(which were actually more relevant to hiding/showing the taskbar), I came through this: 在对stackoverflow进行了许多回答之后(实际上与隐藏/显示任务栏更相关),我经历了以下问题:

How do I get the taskbar's position and size? 如何获得任务栏的位置和大小?

But this can't be done in case of UWP apps.Is there any other way to get the Height of the TaskBar. 但是对于UWP应用程序是无法做到的,还有其他方法可以获取TaskBar的高度。

Please Note : The TaskBar is always visible in case of my application.I don't intend to hide it 请注意 :在我的应用程序中TaskBar始终可见。我无意隐藏它

Thanks!! 谢谢!!

Well!! 好!! So after a lot of searching on internet, seeing similar answers on stackoverflow and suggestions, It seems that calculating the TaskBar height in UWP application is not so straight or simple task. 因此,在Internet上进行大量搜索之后,在stackoverflow和建议上看到了类似的答案,似乎在UWP应用程序中计算TaskBar的高度并不是一件简单的事情。 However for my situation I ended up with this work around which works fine. 但是,就我的情况而言,我最终完成了这项工作 ,可以正常工作。 But I will continue to find a proper approach. 但我将继续寻找适当的方法。 Assuming my screen resolution is 1600x900 ,So here is what I did: 假设我的屏幕分辨率是1600x900 ,那么这就是我所做的:

    private void GetScreenDimension()
    {

        //To get Screen Measurements e.g. height, width, X,Y...
        ApplicationView view = ApplicationView.GetForCurrentView();
        //Getting the Window Title Bar height(In my case I get :Top=32,Bottom=860)
        double titleBarHeight = view.VisibleBounds.Top;
        //Getting the TaskBar Height
        double taskBarHeight = view.VisibleBounds.Top + (view.VisibleBounds.Top / 4);
        //Getting the workable Height of the screen ( excluding Task Bar Height)
        double availableheight = GridTimelineContent.ActualHeight - taskBarHeight;
        double availablewidth = GridTimelineContent.ActualWidth;

        if (_viewModel != null)
        {
            _viewModel.AvailableHeight = availableheight;
            _viewModel.AvailableWidth = availablewidth;
            //Getting the actual Physical height (i.e including TitleBar Height and Task Bar Height, gives 900 in my case which is what I wanted)                              
            _viewModel.ActualScreenHeight = view.VisibleBounds.Height + titleBarHeight + taskBarHeight;

            _viewModel.PageWidth = (this as Page).ActualWidth;

        }
    }

Please Note: 请注意:

1) When I run the application with TaskBar Locked(visible), I get view.VisibleBounds.Height as 828 . 1)当我用TaskBar Locked(可见)运行应用程序时,我得到view.VisibleBounds.Height828

2) When I run the application with TaskBar AutoHidden(Invisible), I get view.VisibleBounds.Height as 868 . 2)当我使用TaskBar AutoHidden(Invisible)运行应用程序时,我得到view.VisibleBounds.Height868

Which gave me an idea that 900-868=32 could be Tittle Bar Height, and as I jumped from 828 to 868 after hiding the Task Bar means 868-828=40 could be the Task Bar Height. 这使我想到900-868 = 32可能是Tittle Bar的高度,而当我在隐藏任务栏后从828跳到868时,意味着868-828 = 40可能是Task Bar的高度。

Conclusion: 结论:

Title Bar Height = view.VisibleBounds.Top (Which is 32) 标题栏高度= view.VisibleBounds.Top (哪个为32)

Task Bar Height = view.VisibleBounds.Top (Which is 32) + (view.VisibleBounds.Top / 4) (Which is 8) ; 任务栏高度= view.VisibleBounds.Top (这是32) +(view.VisibleBounds.Top/4) (这是8) (32+8 = Total 40) (32 + 8 =总数40)

Remainning Height = view.VisibleBounds.Height (Which is 828) 剩余高度= view.VisibleBounds.Height (哪个是828)

If I combine the above three, I get 900 (Required Height) using this line of code: 如果将以上三个结合起来,则使用以下代码行可获得900(所需高度):

_viewModel.ActualScreenHeight = view.VisibleBounds.Height + titleBarHeight + taskBarHeight; _viewModel.ActualScreenHeight = view.VisibleBounds.Height + titleBarHeight + taskBarHeight;

I hope it's useful for others too. 我希望它对其他人也有用。 Thanks!! 谢谢!!

It can't be done simply because not every platform where UWP apps are supported even has a Desktop or TaskBar (and the desktop doesn't count as one of the device capabilities such as camera, microphone, movement or location sensors)! 不能仅仅因为并非所有支持UWP应用程序的平台都具有桌面或TaskBar(而桌面不算作摄像头,麦克风,运动或位置传感器等设备功能之一)就不能做到这一点!

If you need to access the Desktop you will have to create a desktop app. 如果您需要访问桌面,则必须创建一个桌面应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM