简体   繁体   English

WinUI 3 NavigationView 始终使用操作系统的背景色,而不是所选应用主题的背景色

[英]WinUI 3 NavigationView always uses background color from operating system, not from the selected app theme

I am using WinUI 3 with Microsoft App SDK version 1.0.1.我将 WinUI 3 与 Microsoft App SDK 版本 1.0.1 一起使用。

I have issues with the background color of an NavigationView ( MSDN - NavigationView ) element I use for the main navigation.我对用于主导航的NavigationView ( MSDN - NavigationView ) 元素的背景颜色有疑问。

The problem is that the background color of the NavigationView is always the background color of the windows setting (I use windows 10), ie the background color is dark in "dark mode" or white / light gray in "light mode" independently from the setting / theme I use in the app itself.问题是NavigationView的背景颜色始终是 windows 设置的背景颜色(我使用 windows 10),即背景颜色在“深色模式”下为深色或在“浅色模式”下为白色/浅灰色独立于我在应用程序本身中使用的设置/主题。

In the app, I have a small menu to switch themes, with the settings "light", "dark" or "use windows setting".在应用程序中,我有一个小菜单来切换主题,设置为“亮”、“暗”或“使用 windows 设置”。

If I use a "light" setting in windows and change the theme within the app from "light" to "dark" all elements of my app change colors correclty (ie use a dark background and a light foreground color), except the background if the NavigationView which keeps its "light" background in that case.如果我在 windows 中使用“浅色”设置并将应用程序中的主题从“浅色”更改为“深色”,我的应用程序的所有元素都会更改 colors correclty(即使用深色背景和浅色前景色),除了背景如果在这种情况下保持其“浅色”背景的NavigationView Interestingly the foreground color of the font does change in that case from dark to light, which causes the NavigationView to have "light" background color and "light" foreground color.有趣的是,在这种情况下,字体的前景色确实会从深色变为浅色,这会导致 NavigationView 具有“浅色”背景色和“浅色”前景色。

If the windows setting is "dark", the NavigationView has a always dark background color, no matter if I change the app theme to "light" or not.如果 windows 设置为“深色”,则无论我是否将应用程序主题更改为“浅色”, NavigationView的背景色始终为深色。

This is my main window with the NavigationView in the top section (I removed the parts that are not relevant):这是我的主要 window,顶部有NavigationView (我删除了不相关的部分):

<Window>
    <Grid x:Name="Root" x:FieldModifier="Internal">

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- https://learn.microsoft.com/en-us/windows/apps/design/controls/navigationview -->
        <NavigationView x:Name="navigationView" PaneDisplayMode="Top" 
                        IsBackEnabled="True" IsBackButtonVisible="Collapsed" BackRequested="NavigationView_OnBackRequested"
                        SelectionChanged="NavigationView_OnSelectionChanged"
        >
        <NavigationView.MenuItems>
            <NavigationViewItem  Content="A" />
            <NavigationViewItem  Content="B" />
            <NavigationViewItem  Content="C" />
        </NavigationView.MenuItems>

    </NavigationView>


    </Grid>
</Window>

This is the code I use in my app to set the theme:这是我在我的应用程序中用来设置主题的代码:

    public async Task SetThemeAsync(ElementTheme theme)
    {
        _rootElement.RequestedTheme = theme;
    }

The _rootElement points to App.NavigationRootWindow.Root. _rootElement指向 App.NavigationRootWindow.Root。 Please note the the Grid in my XAML code above has sets x:Name="Root" .请注意上面我的 XAML 代码中的Grid设置了x:Name="Root" So the _rootElement where I apply the theme is the Grid of my main window.因此,我应用主题的_rootElement是我的主要 window 的网格。

Does anyone know why the background color of the NavigationView is not changed correctly as I described above?有谁知道为什么NavigationView的背景颜色没有像我上面描述的那样正确更改? Thank you for your time.感谢您的时间。

Based on this issue: Change theme programmatically in WinUI 3 preview 3 desktop application , you could set the Background property on the root element like this:基于这个问题: Change theme programmatically in WinUI 3 preview 3 desktop application ,您可以像这样在根元素上设置Background属性:

Background="{ThemeResource ApplicationPageBackgroundThemeBrush}

The root element is the Grid with the x:Name="Root" in case of the question.在出现问题时,根元素是带有x:Name="Root"Grid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向防病毒或操作系统请求权限以执行 - Request permission from antivirus or operating system to execute .NET线程是否与操作系统线程不同? - Are .NET threads different from operating system threads? 从菜单项中选择图像时如何更改表格的背景色 - how to change background color of form when image selected from menuitems Thread.Sleep在后台线程中使用一个系统处理器中的100% - Thread.Sleep uses 100% from one system processor in a background thread 在WPF中将颜色定义为主题中的系统颜色 - Define a color as a system color within a theme in WPF 操作系统不支持的选定整理顺序。 WinXP的 - Selected collating sequence not supported by the operating system. WinXP 如何将窗口背景绑定到主题的窗口背景颜色? - How to bind Window background to a theme's Window background color? 如何从 WinUI 3 项目中引用 .Net 6.0 项目? - How to reference .Net 6.0 project from WinUI 3 project? 如何更改选定树节点的背景颜色? - How to change background color of selected tree node? 运行涉及远程对象的nunit测试会引发错误操作系统无法运行。 (来自HRESULT的异常:0x800700B6) - Running nunit tests involving remote objects are throwing the error The operating system cannot run . (Exception from HRESULT: 0x800700B6)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM