1

我试图在我的 WP 8 中使用 ThemeManager 来更改一些默认样式。我有一个资源文件,其中包含我的颜色等自定义设置。

我的 ThemeResources.xml

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:System="clr-namespace:System;assembly=mscorlib">
    <Color x:Key="TestColor">#FF2c5f8c</Color>
    <SolidColorBrush x:Key="TestBrush" Color="{StaticResource TestColor}"/>
</ResourceDictionary>

现在在我的 App.xml 中将其设置为合并字典:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="Themes/ThemeResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <local:LocalizedStrings xmlns:local="clr-namespace:MyApp" x:Key="LocalizedStrings"/>
    </ResourceDictionary>
</Application.Resources>

在我的 App.cs 中,在我的应用构造函数中,我使用了 themeManager:

public App()
{
    // Global handler for uncaught exceptions.
    UnhandledException += Application_UnhandledException;

    // Standard XAML initialization
    InitializeComponent();

    // Phone-specific initialization
    InitializePhoneApplication();

    // Language display initialization
    InitializeLanguage();

    // Get the custom theme
    var rd = App.Current.Resources.MergedDictionaries[0];

    // Set custom Theme, fallback to dark
    ThemeManager.SetCustomTheme(rd, Theme.Light);
    ...

最后,在我的 MainPage.xml 中,我使用了这个在 ThemeResources.xml 中定义的 TestBrush,如下所示:

<TextBlock Text="Testing" Foreground="{StaticResource TestBrush}"/>

一切对我来说都是正确的,但是当我尝试运行该应用程序时,出现以下异常:

$exception {System.Windows.Markup.XamlParseException:找不到具有名称/键 TestBrush 的资源 [行:90 位置:175]

在 Visual Studio 设计器中,它显示颜色正确性。

那里可能有什么问题?

编辑:是的,ThemeResources.xml 文件的构建操作设置为“资源”。还是同样的问题。

4

1 回答 1

1

阅读 ThemeManager 自述文件 ( https://github.com/jeffwilcox/wp-thememanager ):

“注意:不要在修改后的 ThemeResources.xaml 中添加任何其他内容(如果将其放在 MergedDictionary 部分中),因为该过程的一部分是在设置主题后删除 MergedDictionary。出于某种原因,如果您不要PhoneForegroundBrush没有保持设置。”

我自己还没有尝试过,但我想如果你想保留你的 TestBrush,那么你需要在一个单独的 XAML 文件中定义它。否则,只需重新定义一个标准主题画笔并使用它。

于 2014-03-26T11:42:07.560 回答