【问题标题】:Why is the TitleBar title gone after adding acrylic effect?为什么添加亚克力效果后 TitleBar 标题不见了?
【发布时间】:2020-04-16 09:14:02
【问题描述】:

我在 MainPage.xaml.cs 中使用以下代码在 TitleBar 上添加了亚克力效果

public void AcrylicTitleBar()
    {
        CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
        var title = ApplicationView.GetForCurrentView();
        title.TitleBar.ButtonBackgroundColor = Colors.Transparent;
        title.TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
        title.TitleBar.ButtonForegroundColor = (Color)Resources["SystemBaseHighColor"];
    }

但我不知道为什么 TitleBar 标题不见了。

如果我删除上面的代码,TitleBar 标题就会回来。有人可以帮帮我吗?

【问题讨论】:

  • 请将代码发布为文本,而不是图像。

标签: c# .net xaml uwp


【解决方案1】:

您的代码没有对您的应用标题栏应用任何“亚克力”效果。 它只是将按钮背景颜色设置为“透明”,这与亚克力相差无几。

为了有一个亚克力标题栏,你需要做以下事情:

在您的 MainPage.xaml.cs 中,添加隐藏默认标题栏的代码并更新其布局,使其仍然像默认工具栏一样工作:

public MainPage()
{
    // Hide default title bar.
    var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
    coreTitleBar.ExtendViewIntoTitleBar = true;
    UpdateTitleBarLayout(coreTitleBar);
}

private void UpdateTitleBarLayout(CoreApplicationViewTitleBar coreTitleBar)
{
    // Get the size of the caption controls area and back button 
    // (returned in logical pixels), and move your content around as necessary.
    LeftPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayLeftInset);
    RightPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayRightInset);
    TitleBarButton.Margin = new Thickness(0, 0, coreTitleBar.SystemOverlayRightInset, 0);

    // Update title bar control size as needed to account for system size changes.
    AppTitleBar.Height = coreTitleBar.Height;
}

private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
    UpdateTitleBarLayout(sender);
}

接下来,在您的MainPage.xaml 中添加AcrylicBrush 作为mainparent 网格背景(注意:该网格应该包含页面中的所有内容) :

<Grid>
  <Grid.Background>
    <AcrylicBrush BackgroundSource="HostBackdrop" 
                  TintColor="{ThemeResource SystemAltHighColor}" 
                  FallbackColor="{ThemeResource SystemAltHighColor}" 
                  TintOpacity="0.5">
     </AcrylicBrush>
  </Grid.Background>
 </Grid>

希望这会有所帮助!

【讨论】:

  • Fwiw,这是实际的系统资源:&lt;AcrylicBrush x:Key="SystemControlAcrylicWindowBrush" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeAltHighColor}" TintOpacity="0.8" FallbackColor="{StaticResource SystemChromeMediumColor}" /&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多