问题描述
我正在制作一个 UWP 应用程序.我的要求是以编程方式获取任务栏的大小(此应用程序将在不同分辨率的平板电脑上运行).在遵循stackoverflow上的许多答案(实际上与隐藏/显示任务栏更相关)之后,我解决了这个问题:
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). After following many answers on stackoverflow(which were actually more relevant to hiding/showing the taskbar), I came through this:
但是在UWP应用程序的情况下无法做到这一点.有没有其他方法可以获取TaskBar的高度.
But this can't be done in case of UWP apps.Is there any other way to get the Height of the TaskBar.
请注意:任务栏在我的应用程序中始终可见.我不打算隐藏它
Please Note : The TaskBar is always visible in case of my application.I don't intend to hide it
谢谢!!
推荐答案
好吧!!所以在网上搜索了很多,在stackoverflow上看到了类似的答案和建议,似乎在UWP应用程序中计算TaskBar高度并不是那么直接或简单的任务.然而,对于我的情况,我最终得到了这个解决方法,效果很好.但我会继续寻找合适的方法.假设我的屏幕分辨率是 1600x900,那么我所做的就是:
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;
}
}
请注意:
1) 当我在 TaskBar Locked(visible) 运行应用程序时,我得到 view.VisibleBounds.Height 为 828.
1) When I run the application with TaskBar Locked(visible), I get view.VisibleBounds.Height as 828.
2) 当我使用 TaskBar AutoHidden(Invisible) 运行应用程序时,我得到 view.VisibleBounds.Height 为 868.
2) When I run the application with TaskBar AutoHidden(Invisible), I get view.VisibleBounds.Height as 868.
这让我想到 900-868=32 可能是标题栏高度,当我隐藏任务栏后从 828 跳到 868 意味着 868-828=40 可以是任务栏高度.
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.
结论:
标题栏高度 = view.VisibleBounds.Top (即 32)
Title Bar Height = view.VisibleBounds.Top (Which is 32)
任务栏高度 = view.VisibleBounds.Top (即 32) + (view.VisibleBounds.Top/4)(即 8);(32+8 = 总共 40)
Task Bar Height = view.VisibleBounds.Top (Which is 32) + (view.VisibleBounds.Top / 4)(Which is 8);(32+8 = Total 40)
剩余高度 = view.VisibleBounds.Height (即 828)
Remainning Height = view.VisibleBounds.Height (Which is 828)
如果我将以上三者结合起来,我使用这行代码得到 900(要求的高度):
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!!
这篇关于UWP:如何获取任务栏高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!