在创建标题栏之前以编程方式获取默认的TitleBar高度?

时间:2017-02-06 11:38:19

标签: c# uwp titlebar custom-titlebar

我正在为我的uwp应用创建一个自定义标题栏。我想匹配系统栏的高度。

可能能够达到那个高度

CoreApplication.GetCurrentView().TitleBar.Height

但这取决于很多事情。标题栏可能尚未调整大小。

我还看到了一个建议(来自winforms)来查看窗口顶部和内容视图顶部的y坐标的差异。但这再次看起来很可疑。首先,一旦我将ExtendViewIntoTitleBar设置为true,我认为该方法不会起作用。

是否有可靠的方式以编程方式获取默认高度?

1 个答案:

答案 0 :(得分:1)

我知道这个答案可能对当时最初问这个问题的人没有用,但是我仍然想提出答案:

您可以在标题栏大小更改时注册处理程序。 (文档中提到了尺寸变化,但可能只是标题按钮的偏移量而不是高度) 至少这段时间,这段代码对我来说很有效

In [406]: for x in ['one','tow','three']: 
     ...:     %run echo.py -x x 
     ...:                                                                                              
['echo.py', '-x', 'x']
['echo.py', '-x', 'x']
['echo.py', '-x', 'x']
In [407]: for x in ['one','tow','three']: 
     ...:     %run echo.py -x $x 
     ...:                                                                                              
['echo.py', '-x', 'one']
['echo.py', '-x', 'tow']
['echo.py', '-x', 'three']

只要titleBar的尺寸发生变化(例如DPI中的变化),以上代码就会自动调用您的函数(//Put the below line in the Page initialization/OnNavigated function var coreTitleBar = CoreApplication.GetCurrentView().TitleBar; coreTitleBar.ExtendViewIntoTitleBar = true; coreTitleBar.LayoutMetricsChanged += CoreTitleBar_LayoutMetricsChanged; private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args) { MyAppTitleBar.Height = sender.Height; } 。在这里,CoreTitleBar_LayoutMetricsChanged是我为自定义标题栏制作的MyAppTitleBar

可以找到更多信息here

相关问题