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;
}