简体   繁体   中英

Get default TitleBar height programmatically before a titlebar is created?

I am creating a custom title bar for my uwp app. 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. But again that seems fishy. For one thing, once I've set ExtendViewIntoTitleBar to true, I don't think the method would work.

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). Here, MyAppTitleBar is a Grid I made for my custom title bar.

Futher info can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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