如何在 Windows Phone 8.1 Silverlight App 中将颜色代码分组到一个文件中
How to group color codes into one single file in a Windows Phone 8.1 Silverlight App
我有一个 Windows Phone 8.1 Silverlight 应用程序,使用 Caliburn.Micro 框架。
Atm 我所有的颜色代码都被硬编码到它们被使用的地方。
Foreground="#c8d75a"
这意味着我在我的应用程序中对大约 150 个位置进行了硬编码。
所以我想我会将所有颜色分组到一个文件中,然后在我的 xaml 页中引用该颜色。
我做了很多 Google 搜索,他们都找到了答案 "Use a Resource Directory" 然后在我的 xaml 页面中我可以使用变量目录就像我对任何其他静态资源一样
{StaticResource LightGreen}
我的问题是我没有任何称为资源目录的模板。所以我的问题是:
这甚至可以在 Windows Phone 8.1 Silverlight 应用程序中添加资源目录吗?如果不是,那我应该用什么?
感谢您的宝贵时间。
当然可以。我不知道为什么你没有 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>
然后您需要将创建的 ResourceDictionary
包含到您的 App.xaml
:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Constants.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
或者如果您想在页面中包含字典:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Constants.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
我有一个 Windows Phone 8.1 Silverlight 应用程序,使用 Caliburn.Micro 框架。
Atm 我所有的颜色代码都被硬编码到它们被使用的地方。
Foreground="#c8d75a"
这意味着我在我的应用程序中对大约 150 个位置进行了硬编码。
所以我想我会将所有颜色分组到一个文件中,然后在我的 xaml 页中引用该颜色。
我做了很多 Google 搜索,他们都找到了答案 "Use a Resource Directory" 然后在我的 xaml 页面中我可以使用变量目录就像我对任何其他静态资源一样
{StaticResource LightGreen}
我的问题是我没有任何称为资源目录的模板。所以我的问题是: 这甚至可以在 Windows Phone 8.1 Silverlight 应用程序中添加资源目录吗?如果不是,那我应该用什么?
感谢您的宝贵时间。
当然可以。我不知道为什么你没有 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>
然后您需要将创建的 ResourceDictionary
包含到您的 App.xaml
:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Constants.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
或者如果您想在页面中包含字典:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Constants.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>