UWP: How to get the TaskBar Height

◇◆丶佛笑我妖孽 提交于 2019-12-02 10:56:36

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. 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:

    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.

2) When I run the application with TaskBar AutoHidden(Invisible), I get view.VisibleBounds.Height as 868.

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.

Conclusion:

Title Bar Height = view.VisibleBounds.Top (Which is 32)

Task Bar Height = view.VisibleBounds.Top (Which is 32) + (view.VisibleBounds.Top / 4)(Which is 8);(32+8 = Total 40)

Remainning Height = view.VisibleBounds.Height (Which is 828)

If I combine the above three, I get 900 (Required Height) using this line of code:

_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)!

If you need to access the Desktop you will have to create a desktop app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!