如何将颜色代码分组到Windows Phone 8.1 Silverlight应用程序中的单个文件中

use*_*952 2 silverlight xaml caliburn.micro windows-phone-8.1

我有一个使用Caliburn.Micro框架的Windows Phone 8.1 Silverlight应用程序.

Atm我的所有颜色代码都硬编码到他们使用的地方.

Foreground="#c8d75a"
Run Code Online (Sandbox Code Playgroud)

这意味着我的应用程序中有大约150个地方硬编码的颜色代码.

所以我想我会将所有颜色分组到一个文件中,然后在我的xaml页面中引用颜色.

我已经做了很多谷歌搜索,他们都回答"使用资源目录",然后在我的xaml页面中,我将能够使用目录中的变量,就像我对任何其他静态resoruce一样

{StaticResource LightGreen}
Run Code Online (Sandbox Code Playgroud)

我的问题是我没有任何名为资源目录的模板.所以我的问题是:在Windows Phone 8.1 Silverlight应用程序中甚至可以添加资源目录吗?如果不是我应该使用什么呢?

感谢您的时间.

Kri*_*sic 6

当然有可能.我不知道你为什么没有文件模板,ResourceDictionary但你可以自己创建一个.

假设您Resources的主项目中有一个文件夹,那么您需要创建一个带.xaml扩展名的文件Constants.xaml.您可以在visual studio外部执行此操作,然后将文件复制到项目中.

该文件的内容应如下所示:

<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">

    <!-- SOCIAL NETWORKS -->
    <Color x:Key="FacebookColor">#3B5998</Color>
    <Color x:Key="GoogleColor">#DB4A39</Color>
    <Color x:Key="TwitterColor">#00A0D1</Color>

    <SolidColorBrush x:Key="FacebookBrush" Color="{StaticResource FacebookColor}"/>
    <SolidColorBrush x:Key="GoogleBrush" Color="{StaticResource GoogleColor}"/>
    <SolidColorBrush x:Key="TwitterBrush" Color="{StaticResource TwitterColor}"/>

    <!-- BOOLEANS -->
    <system:Boolean x:Key="BoolTrue">True</system:Boolean>
    <system:Boolean x:Key="BoolFalse">False</system:Boolean>

    <!-- COLORS -->
    <Color x:Key="LightGreen">#c8d75a</Color>

    <!-- BRUSHES -->
    <SolidColorBrush x:Key="LightGreenBrush" Color="{StaticResource LightGreen}"/>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

然后你需要将创建包括在ResourceDictionary你的App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Constants.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

或者如果要在页面中包含字典:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Constants.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)