简体   繁体   English

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

[英]Get default TitleBar height programmatically before a titlebar is created?

I am creating a custom title bar for my uwp app.我正在为我的 uwp 应用程序创建一个自定义标题栏。 I want to match the height of the system bar.我想匹配系统栏的高度。

I might be able to get that height be calling也许能够得到那个高度在呼唤

CoreApplication.GetCurrentView().TitleBar.Height

But that depends on a lot of things.但这取决于很多事情。 The title bar may not have been sized yet.标题栏可能尚未调整大小。

I've also seen a suggestion (from winforms) to look at the difference of the y coordinates of the window top and the content view top.我还看到了一个建议(来自 winforms)来查看窗口顶部和内容视图顶部的 y 坐标的差异。 But again that seems fishy.但这又似乎很可疑。 For one thing, once I've set ExtendViewIntoTitleBar to true, I don't think the method would work.一方面,一旦我将 ExtendViewIntoTitleBar 设置为 true,我认为该方法将不起作用。

Is there reliable way to programmatically get the default height?是否有可靠的方法以编程方式获取默认高度?

I know that this answer might not be useful to the person who initially asked, at this point of time, but I would still like to suggest the answer:我知道这个答案在这个时候可能对最初提出问题的人没有用,但我仍然想建议答案:

You can register a handler for when the size of title bar changes.您可以在标题栏的大小更改时注册处理程序。 (The docs mention size change, but it may only be the caption button offset and not height) This piece of code works well for me, at least at the moment (文档提到大小更改,但它可能只是标题按钮偏移而不是高度)这段代码对我来说效果很好,至少目前

//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;
}

The above code calls your function ( CoreTitleBar_LayoutMetricsChanged ) automatically whenever the dimensions of the titleBar change (like change in DPI).每当CoreTitleBar_LayoutMetricsChanged的尺寸发生变化(例如 DPI 的变化)时,上面的代码CoreTitleBar_LayoutMetricsChanged自动调用您的函数( CoreTitleBar_LayoutMetricsChanged )。 Here, MyAppTitleBar is a Grid I made for my custom title bar.在这里, MyAppTitleBar是我为自定义标题栏制作的Grid

Futher info can be found here更多信息可以在这里找到

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

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