【问题标题】:How do I customize the Window Titlebar in a Windows App SDK (WIN UI/Project Reunion) Project?如何在 Windows App SDK (WINUI/Project Reunion) 项目中自定义窗口标题栏?
【发布时间】:2021-11-15 16:38:35
【问题描述】:

我正在尝试使用新的 Widows App SDK 构建应用程序。我使用 Windows 社区工具包来创建应用程序。

咨询documentation后,我尝试了这个:

在我的应用显示的第一页上,我创建了一个文本块:

<TextBlock Text="Hello" x:Name="CustomTitleBar" /> 

在这个页面后面的代码中,我添加了如下代码:

 private void Page_Loaded(object sender, RoutedEventArgs e)
 {
   App.MainWindow.ExtendsContentIntoTitleBar = true;
   App.MainWindow.SetTitleBar(CustomTitleBar);
   App.MainWindow.Activate();
 }

在 App XAML 页面上,我按照文档的说明覆盖了这些值:

<SolidColorBrush x:Key="WindowCaptionBackground">Green</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">LightGreen</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForeground">Red</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForegroundDisabled">Pink</SolidColorBrush> 

以上内容确实使默认标题栏消失了。但是,我只剩下“你好”这个词,没有背景或按钮:

我有什么遗漏吗?

【问题讨论】:

  • 您的PageWindow 是如何定义的?
  • 主窗口在 App.xaml.cs 文件中定义为 public static Window MainWindow { get;放; } = new Window() { Title = "AppDisplayName".GetLocalized() };
  • 我正在使用我的应用程序中显示的第一页。它在代码隐藏中是这样定义的:公共密封部分类 MainSelectionPage : Page。在匹配的 XAML 中,它的定义如下
  • 我将文本块放在应用程序中显示的第一页上。 (这是问题吗?)

标签: xaml windows-community-toolkit winui-3 windows-template-studio windows-app-sdk


【解决方案1】:

您可能应该使用AppWindow 和presenter(在我的例子中我使用OverlapedPresenter)类。这是我在应用中使用的一些代码。

public MainWindow()
{
   InitializeComponent();
   AppWindow appWindow = GetAppWindowForCurrentWindow();
   appWindow.TitleBar.ExtendsContentIntoTitleBar = false;
}

然后,在构造函数下,我使用微软在this 示例中提供的方法:

private AppWindow GetAppWindowForCurrentWindow()
{
   IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
   WindowId myWndId = Win32Interop.GetWindowIdFromWindow(hWnd);
   return AppWindow.GetFromWindowId(myWndId);
}

我使用OverlappedPresenter 类来更改窗口的一些内容,比如它是否可以调整大小。

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2021-07-14
    • 2010-09-11
    • 2010-12-12
    • 2022-10-31
    • 1970-01-01
    • 2018-02-25
    • 2012-02-19
    • 2014-07-05
    相关资源
    最近更新 更多