【发布时间】:2019-03-13 08:17:31
【问题描述】:
碰巧我为应用程序颜色定义了一个ResourceDictionary 以在 XAML 文件中使用它,并为这些颜色提供了一个静态类以在 cs 代码中使用:
例如:
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Themes.AppTheme">
<Color x:Key="BrandColor">#ffd92e</Color>
<Color x:Key="BgColor">#343433</Color>
</ResourceDictionary>
和对面的类:
public static class AppColors
{
public static readonly Color BrandColor = (Color)Application.Current.Resources["BrandColor"];
public static readonly Color BGColor = (Color)Application.Current.Resources["BgColor"];
}
另一种情况,我在 xaml 和 cs 中都使用图标字体,
在 XAML 中它看起来像 &#xe8d5;,在 cs 中它是:\ue8d5。我想将它们保存在一个文件中,我可以在其中通过 XAML 和 cs 中有意义的名称来引用它们,例如:
IconBell = \ue8d5
是否可以在一个地方定义类似的资源并在 XAML 和代码中使用它们?
【问题讨论】:
-
是的,有可能。但是请确保静态类
AppColors可以调用所需的Resources[...]。 Xaml 直接使用{StaticResource TabColor},.CS 使用Resources ["searchBarStyle"]或您定义的静态类(Label.TextColor = AppColors.BrandColor)。如果需要转换值,您可以使用IValueConverter来做到这一点。
标签: xamarin xamarin.forms resourcedictionary