Windows Universal App SetTitleBar

2019-05-31 09:25发布

问题:

I have a question about the Title'Bar sample from Microsoft's GitHub repository(https://github.com/JustinXinLiu/FullScreenTitleBarRepo/tree/master/FullScreenTitleBarRepo): In the AddCustomTitleBar function,there has a line:

customTitleBar.EnableControlsInTitleBar(areControlsInTitleBar); 

EnableControlsInTitleBar is here:

public void EnableControlsInTitleBar(bool enable)
{
    if (enable)
    {
        TitleBarControl.Visibility = Visibility.Visible;
        // Clicks on the BackgroundElement will be treated as clicks on the title bar.
        Window.Current.SetTitleBar(BackgroundElement);
    }
    else
    {
        TitleBarControl.Visibility = Visibility.Collapsed;
        Window.Current.SetTitleBar(null);
    }
}

but if I don't call the fuction(EnableControlsInTitleBar),the sample still work well

In Justin XL sample(https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/TitleBar) there indeed have use this:

Window.Current.SetTitleBar(BackgroundElement);

that puzzled me,hope somebody can give me a explain,thanks.

回答1:

"but if I don't call the fuction(EnableControlsInTitleBar),the sample still work well"

I don't think it's the truth. There are several places will call this function, so I think you didn't comment out all of them.

The following line is used to let your custom title bar can handle input(for example the mouse click) like default title bar.

Window.Current.SetTitleBar(BackgroundElement);

Take the MS sample as example here. If we launch the MS sample without any change, you can do the following actions:

Select "2) Custom drawing" -> check "Extend view into title bar" -> check "Enable controls in title bar" -> you will see the following title bar and be able to check the checkbox on title bar.

But if we comment out the SetTitleBar call, the checkbox will not response to your mouse click.