C# UWP RequestedTheme在ListView和ListViewItem中不一致
我的有一个,基本上是一个列表视图C# UWP RequestedTheme在ListView和ListViewItem中不一致,c#,uwp,win-universal-app,C#,Uwp,Win Universal App,我的有一个,基本上是一个列表视图 <local:PlaylistControl x:Name="FullPlaylistControl" Margin="10,10,10,0" AllowReorder="True" AlternatingRowColor="False" Background="Transparent" IsNowPlaying="True" RequestedTheme="Dark" /> 在的load事件
<local:PlaylistControl
x:Name="FullPlaylistControl"
Margin="10,10,10,0"
AllowReorder="True"
AlternatingRowColor="False"
Background="Transparent"
IsNowPlaying="True"
RequestedTheme="Dark" />
在的load
事件中,我调用了一个函数SetTextColor
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MediaHelper.SwitchMusicListeners.Add(this);
SetTextColor(MediaHelper.CurrentMusic);
}
public void SetTextColor(Music music)
{
if (Data == music)
{
TitleTextBlock.Foreground = ArtistTextButton.Foreground = AlbumTextButton.Foreground = DurationTextBlock.Foreground =
LongArtistTextButton.Foreground = LongArtistAlbumPanelDot.Foreground = LongAlbumTextButton.Foreground = ColorHelper.HighlightBrush;
TextColorChanged = true;
}
else if (TextColorChanged)
{
if (RequestedTheme == ElementTheme.Dark)
{
TitleTextBlock.Foreground = ColorHelper.WhiteBrush;
ArtistTextButton.Foreground = AlbumTextButton.Foreground = DurationTextBlock.Foreground =
LongArtistTextButton.Foreground = LongArtistAlbumPanelDot.Foreground = LongAlbumTextButton.Foreground = ColorHelper.GrayBrush;
}
else
{
TitleTextBlock.Foreground = ArtistTextButton.Foreground = AlbumTextButton.Foreground = DurationTextBlock.Foreground =
LongArtistTextButton.Foreground = LongArtistAlbumPanelDot.Foreground = LongAlbumTextButton.Foreground = ColorHelper.BlackBrush;
}
TextColorChanged = false;
}
}
我的问题是,为什么在加载的
事件中调用的SetTextColor
中的RequestedTheme
的值为ElementTheme.Default
而不是ElementTheme.Dark
?播放控制项的请求主题
何时保持暗
的值,以便正确设置我的文本颜色?您最好使用XAML中的资源,而不是代码中的资源来处理此问题,请参阅:
但要回答你的问题,这是预期的行为ElementTheme.Default
仅表示元素的主题未被覆盖,正在使用默认的应用程序主题。其他两个值表示元素的主题已被重写App.Current.RequestedTheme
给出应用程序的实际主题FrameworkElement.RequestedTheme
的值始终为Default
,除非您在该特定元素上明确将其设置为其他值。它的所有子项仍将具有值Default
注意,您应该与reactiveme
,而不是RequestedTheme
,因为如果值仍然ElementTheme.Default
,则可能会导致它使用与应用程序不同的主题
下面的方法可能会帮助您获得适当的亮/暗值
public static ElementTheme GetEffectiveTheme(FrameworkElement e)
{
if (e.ActualTheme == ElementTheme.Default)
return App.Current.RequestedTheme == ApplicationTheme.Dark ? ElementTheme.Dark : ElementTheme.Light;
return e.ActualTheme;
}
但是,也要使用更多的资源。它们会在主题更改时自动重新计算,无需任何代码或事件侦听器。非常感谢!使用actualteme
拯救了我的生命!我实际上是在xaml中使用ThemeResources
,但有时我需要TextBlock
来切换颜色,所以我还需要一种编程方式来实现这一点。
public static ElementTheme GetEffectiveTheme(FrameworkElement e)
{
if (e.ActualTheme == ElementTheme.Default)
return App.Current.RequestedTheme == ApplicationTheme.Dark ? ElementTheme.Dark : ElementTheme.Light;
return e.ActualTheme;
}