Windows 通用应用程序 SetTitleBar

Windows Universal App SetTitleBar

我对 Microsoft GitHub 存储库中的 Title'Bar 示例有疑问(https://github.com/JustinXinLiu/FullScreenTitleBarRepo/tree/master/FullScreenTitleBarRepo):在 AddCustomTitleBar 函数中,有一行:

customTitleBar.EnableControlsInTitleBar(areControlsInTitleBar); 

EnableControlsInTitleBar 在这里:

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

但如果我不调用函数 (EnableControlsInTitleBar),示例仍然可以正常工作

在 Justin XL 示例 (https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/TitleBar) 中确实有这样的用法:

Window.Current.SetTitleBar(BackgroundElement);

很疑惑,希望有大佬能给个解释,谢谢

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

我不认为这是真的。有几个地方会调用这个函数,所以我想你没有把它们都注释掉。

下一行用于让您的自定义标题栏可以像默认标题栏一样处理输入(例如鼠标单击)。

Window.Current.SetTitleBar(BackgroundElement);

这里以MS样本为例。如果我们在没有任何更改的情况下启动 MS 示例,您可以执行以下操作:

Select "2) 自定义绘图" -> 检查 "Extend view into title bar" -> 检查 "Enable controls in title bar" -> 你会看到下面的标题栏,并且可以勾选标题栏上的复选框。

但是如果我们注释掉 SetTitleBar 调用,复选框将不会响应您的鼠标单击。