如何使用代码(UWP)覆盖ThemeDictionary中的资源

时间:2017-05-23 11:25:22

标签: c# xaml uwp

我想拥有相同的XAML,但是在C#代码中:

<Application.Resources>

    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="SystemControlHighlightBaseMediumLowBrush" Color="White" />
                <SolidColorBrush x:Key="SystemControlHighlightBaseHighBrush" Color="White" />
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>

    </ResourceDictionary>

</Application.Resources>

2 个答案:

答案 0 :(得分:0)

将以下内容添加到您的应用OnLaunched方法:

ResourceDictionary lightTheme = new ResourceDictionary();
lightTheme["SystemControlHighlightBaseMediumLowBrush"] = new SolidColorBrush(Windows.UI.Colors.White);
lightTheme["SystemControlHighlightBaseHighBrush"] = new SolidColorBrush(Windows.UI.Colors.White);
App.Current.Resources.ThemeDictionaries.Add("Light", lightTheme);

答案 1 :(得分:-1)

您可以使用

Brush SystemControlHighlightBaseHighBrush = new SolidColorBrush(Colors.White);
Brush SystemControlHighlightBaseHighBrush = new SolidColorBrush(Colors.White);

然后您可以像这样使用它来设置按钮的颜色

myButton.Background = SystemControlHighlightBaseHighBrush;

如果您的评论建议您想要更改鼠标悬停时的颜色,则需要捕获鼠标悬停事件,然后您可以将颜色更改为其中一个命名样式。例如,要在鼠标悬停时更改按钮颜色,您可以执行此操作

private void myButton_MouseHover(object sender, System.EventArgs e) 
{
    myButton.Background = SystemControlHighlightBaseHighBrush;
}