是否可以将一个颜色资源指向 Xamarin.Forms 中的另一个颜色资源? [英] Is it possible to point one Color resource to another Color resource in Xamarin.Forms?

查看:20
本文介绍了是否可以将一个颜色资源指向 Xamarin.Forms 中的另一个颜色资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Xamarin Forms 应用程序,我目前正在绘制我的应用程序 Resources,主要是我的颜色.

I am building a Xamarin Forms Application and I am currently drawing up my application Resources, mainly my colours.

例如我有以下内容:

  <Color x:Key="Slate">#404040</Color>
  <Color x:Key="Blue">#458623</Color>

  <Color x:Key="SelectedItemColour">#458623</Color>

如您所见,我的 SelectedItemColourBlue 相同.

As you can see my SelectedItemColour is the same as the Blue.

我尝试了以下方法,但没有奏效:

I have tried the following but it didn't work:

  <Color x:Key="Slate">#404040</Color>
  <Color x:Key="Blue">#458623</Color>

  <Color x:Key="SelectedItemColour" Color="{StaticResource Blue}"/>

我知道如果 WPF 你可以做这里

I know if WPF you can do the answer stated here

是否可以在 Xamarin.Forms 中将 颜色资源 指向另一个 颜色资源?

Is it possible to point a Colour Resource to another Colour Resource in Xamarin.Forms?

推荐答案

您可以将 x:Static 与静态类结合使用,以便按名称直接引用这些颜色.这样做的好处是可以将颜色集中到一个类中并最大限度地减少 XAML 的数量.

You can use x:Static in tandem with a static class in order to directly reference those colors by name. This has the benefits of keeping the colors centralized into one class and minimizing the amount of XAML.

namespace ResourceColors
{
    public static class Colors
    {
        public static Color Slate = Color.FromHex("#404040");
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ResourceColors;assembly=ResourceColors" x:Class="ResourceColors.PageOne">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Color x:Key="Blue">#458623</Color>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
            <Label Text="Test" TextColor="{x:Static local:Colors.Slate}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这篇关于是否可以将一个颜色资源指向 Xamarin.Forms 中的另一个颜色资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆