Windows Phone 8、ThemeManager 和主题资源

本文关键字:资源 ThemeManager Phone Windows | 更新日期: 2023-09-27 18:30:54

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

我的主题资源.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>

现在在我的应用程序中.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>

在我的应用程序中.cs,在我的应用程序构造函数中,我使用主题管理器:

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);
    ...

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

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

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

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

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

那里可能有什么问题?

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

Windows Phone 8、ThemeManager 和主题资源

从主题管理器自述文件 (https://github.com/jeffwilcox/wp-thememanager

):

"注意:不要在修改后的 ThemeResources.xaml 中放置您稍后需要的任何其他内容(如果将其放在 MergedDictionary 部分),因为该过程的一部分是在设置主题后删除 MergedDictionary。出于某种原因,如果您不这样做,PhoneForegroundBrush 不会保持设置状态。

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