是否有可能以一种颜色资源指向Xamarin.Forms另一种颜色的资源? [英] Is it possible to point one Color resource to another Color resource in Xamarin.Forms?
问题描述
我建立一个 Xamarin形式
应用程序,我目前正在起草我的应用程序资源
,主要是我的色彩。
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>
正如你可以看到我的 SelectedItemColour
是同为蓝色
我曾尝试以下,但它没有工作:
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}"/>
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?
推荐答案
所以,这样我不得不这样做,这是非常复杂,所以仔细聆听。
So the way I had to do this was very complex so listen carefully.
我发现真正的问题在这里是我需要主题我的Xamarin.Forms的应用程序。
I found that the real problem here was that I needed to "Theme" my Xamarin.Forms application.
因此,继承人什么我。
首先做出了抽象主题
类:
public abstract class Theme
{
public Dictionary<string, Color> Colours { get; set; } = new Dictionary<string, Color>();
protected void DictionaryThemes(Type type)
{
var properties = type.GetRuntimeProperties();
foreach (PropertyInfo propInfo in properties)
{
if (propInfo.PropertyType == typeof(Color))
{
var key = propInfo.Name;
var value = propInfo.GetValue(this);
Colours.Add(key, (Color)value);
}
}
}
public abstract Color TitleColour { get; }
}
通过使用反射一个很好的方法来填充字典充满了我的资源
With a nice method using reflection to fill a dictionary full of my Resources
然后我扩展这个类与我实际的主题:
I then extended this class with my actual theme:
public class MyTheme : Theme
{
public MyTheme()
{
DictionaryThemes(typeof(MyTheme));
}
//Slate
public override Color TitleColour { get; } = Color.FromHex("#00ff00");
}
还是和我在一起?好...
Still with me? good...
然后我不得不加载这个主题到我的应用程序资源
并与我的其他应用程序进行合并资源。我不得不从其他资源分割
,这样我可以在我的主<$ C $使用C>资源更新的文件。 MyTheme的
资源
I then had to load this theme into my Application Resources
and merge it with my other application resources. I have to split the MyTheme
resource from the other Resources
so that I can use it in my main Resources
file later.
让我告诉你,这是我的 CurrentTheme
资源文件:
Let me show you, here is my CurrentTheme
resource file:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:styling="clr-MyApp.Styling;assembly=MyApp"
x:Class="MyApp.Styling.CurrentTheme">
<ContentPage.Resources>
<ResourceDictionary>
<styling:MyTheme x:Key="CurrentTheme"/>
</ResourceDictionary>
我有做一个网页的一部分, Xamarin.Forms
的密封
的 ResourceDictionary中
类的(见这里)这意味着你不能扩展它,并创建自己的..耻辱。 。无论如何,我离题
I have to do it as part of a page as Xamarin.Forms
has Sealed
the ResourceDictionary
class (see here) meaning you can't extend it and create your own.. Shame. Anyway I digress.
然后我把我的应用程序资源,以 CurrentTheme
像这样:
I then set my application Resources to CurrentTheme
like so:
var currentTheme = new CurrentTheme();
Xamarin.Forms.Application.Current.Resources = currentTheme.Resources;
然后我可以在其他风格的合并从我的主资源
文件,在这种情况下,所谓的样式
:
I can then merge in other styles from my main Resource
file, in this case called Styles
:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:styling="clr-MyApp.Styling;assembly=MyApp"
x:Class="MyApp.Styling.Styles">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="TitleStyle" TargetType="Label">
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="FontSize" Value="30" />
<Setter Property="TextColor" Value="{styling:Colour TitleColour}" />
</Style>
</ResourceDictionary>
我合并这为我的主要应用程序资源,像这样:
I merge this into my main application resources like so:
var styles = new Styles();
foreach (var item in styles.Resources)
{
Application.Current.Resources.Add(item.Key,item.Value);
}
现在,你可以看到setter属性之一,在样式
类看起来像这样:
Now as you can see one of the setter properties in the Styles
class looks like so:
和你说什么是价值呢?
下面来拼图的最后一块。扩展方法,它允许您定义 XAML
象上面。
Here comes the final piece of the puzzle. An extension method that allows you to define your xaml
like the above.
首先在 ColourExtension
类:
// You exclude the 'Extension' suffix when using in Xaml markup
[ContentProperty("Text")]
public class ColourExtension : IMarkupExtension
{
public string Text { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
throw new Exception("No Colour Key Provided");
return StylingLookup.GetColour(Text);
}
}
和最后的 StylingLookup
类:
public static class StylingLookup
{
public static Color GetColour(string key)
{
var currentTheme = (Application.Current.Resources["CurrentTheme"] as Theme);
return currentTheme.Colours[key];
}
}
现在,这一切是有道理的,为什么我们不得不拆分 CurrentTheme
从主样式
资源。由于该行的:
And now it all makes sense, Why we had to split CurrentTheme
from the main Styles
resources. Because of the line:
var currentTheme = (Application.Current.Resources["CurrentTheme"] as Theme);
如果任何人有一个更好的模式来的造型的应用我喜欢听到它
If anyone has a better pattern for Styling an application I'd love to hear it
这篇关于是否有可能以一种颜色资源指向Xamarin.Forms另一种颜色的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!