4

在我的 Windows Phone 8 应用程序中,我在/Styles/DefaultStyles.xaml位置的 xaml 文件中定义了一些隐式样式

我有一个类似的文件,但在/Styles/GreenStyles.xaml中定义了不同的颜色、字体等。

我在 App.xaml 中引用了默认样式文件,如下所示:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary   Source="Styles/DefaultStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

我想让我的应用程序以编程方式从其他样式文件(GreenStyles)切换其隐式样式。

我怎样才能做到这一点?

**

更新:

我设法更改资源字典的来源,如下所示:

ResourceDictionary style = App.Current.Resources.MergedDictionaries.ToList()[0];
            string source = String.Format("/ApplicationName;component/Styles/GreenStyles.xaml");
            style.Source = new Uri(source, UriKind.Relative);

注意: component这个词一定要这样写,以免出现异常

现在我有一个问题:当字典的源发生变化时,只有隐式样式(没有x:Key属性的样式)被切换。

具有指定键并在两个文件中定义两次(具有不同属性)的任何其他样式将不会反映在 UI 中。

所以如果我有这些文件: DefaultStyles.xaml:

    <Style x:Key="MainGrid" TargetType="Grid">
        <Setter Property="Background" Value="Red"/>
    </Style>

    <Style  TargetType="TextBlock">
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="FontSize" Value="24"/>
    </Style>
</ResourceDictionary>

并且:GreenStyles.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">

    <Style x:Key="MainGrid" TargetType="Grid">
        <Setter Property="Background" Value="Green"/>
    </Style>

    <Style  TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green"/>
        <Setter Property="FontSize" Value="24"/>
    </Style>
</ResourceDictionary>

并且我将源切换为指向GreenStyles.xaml,任何具有MainGrid样式的 Grid仍将其背景为Red

这可能是什么原因?

4

1 回答 1

0

您可以尝试使用此处描述的方法 Jeff Wilcox:http: //www.jeff.wilcox.name/2012/01/phonethememanager/

此处描述了 Silverlight 的替代方法,我不确定这是否适用于 Windows Phone(尽管它们共享一些代码库): http ://silverlightips.wordpress.com/2010/04/29/change-themestyle-using-合并字典/

如果你有一个大型应用程序,这两种方法都不容易,你可以考虑另一种选择,比如(叫我疯了)

<Button Style="{Binding Locator.Theme, Converter={StaticResource StyleThemeConverter}, ConverterParameter=RefreshButtonStyle}"

希望这可以帮助。

于 2013-12-18T17:29:32.927 回答