[英]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.