define color in Xamarin.Forms XAML without “The type 'Color' does not support direct content”
There are a number of places in the Xamarin docs and blog (here's one, and the docs don't even cover this issue) that refer to color definitions in a ResourceDictionary
that look like this:
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">#33302E</Color>
<Color x:Key="textColor">White</Color>
</ResourceDictionary>
</Application.Resources>
That seems to be old news, however, because if you try that now, you get a compilation error -- The type 'Color' does not support direct content
.
The current norm seems to be to use a FactoryMethod
(like FromHsla
) and pass numeric color arguments. But is there a current concise syntax for defining Color
keys (by name, ideally, like Red
) in a ResourceDictionary
, using XAML?
xaml xamarin.forms
|
show 1 more comment
There are a number of places in the Xamarin docs and blog (here's one, and the docs don't even cover this issue) that refer to color definitions in a ResourceDictionary
that look like this:
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">#33302E</Color>
<Color x:Key="textColor">White</Color>
</ResourceDictionary>
</Application.Resources>
That seems to be old news, however, because if you try that now, you get a compilation error -- The type 'Color' does not support direct content
.
The current norm seems to be to use a FactoryMethod
(like FromHsla
) and pass numeric color arguments. But is there a current concise syntax for defining Color
keys (by name, ideally, like Red
) in a ResourceDictionary
, using XAML?
xaml xamarin.forms
The way you're doing it should work perfectly well. Make sure you're referencing all necessaryxmlns
namespaces.
– jsanalytics
Nov 24 '18 at 22:52
It demonstrably does not work in Xamarin.Forms 3.4 (latest), your jpeg notwithstanding. The only namespace that's referenced is xmlns="xamarin.com/schemas/2014/forms" -- what else are you suggesting is required? I tried referencing System.Color, but the same compilation error comes up -- does not support direct content.
– Graham Charles
Nov 25 '18 at 4:50
Are you missing thehttp://
in your namespace ?
– jsanalytics
Nov 25 '18 at 7:41
No. Here's the entire definition:<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:assets="clr-namespace:MyApp.Assets" x:Class="MyApp.Assets.Colors">
It's not my references -- the syntax simply doesn't work anymore in 3.4. If <Color>Red</Color> still works for you, you're either using an older X.F or some other Color library.
– Graham Charles
Nov 25 '18 at 20:50
It works with both, 3.1 and 3.4.0.1008975. Sometimes VS gets a little cranky when it comes to XAML. One thing you can try is to tell it exactly where to findColor
:xmlns:xyz="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
and then use<xyz:Color/>
. Of course, adjust it for whatever prefix you like.
– jsanalytics
Nov 25 '18 at 22:06
|
show 1 more comment
There are a number of places in the Xamarin docs and blog (here's one, and the docs don't even cover this issue) that refer to color definitions in a ResourceDictionary
that look like this:
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">#33302E</Color>
<Color x:Key="textColor">White</Color>
</ResourceDictionary>
</Application.Resources>
That seems to be old news, however, because if you try that now, you get a compilation error -- The type 'Color' does not support direct content
.
The current norm seems to be to use a FactoryMethod
(like FromHsla
) and pass numeric color arguments. But is there a current concise syntax for defining Color
keys (by name, ideally, like Red
) in a ResourceDictionary
, using XAML?
xaml xamarin.forms
There are a number of places in the Xamarin docs and blog (here's one, and the docs don't even cover this issue) that refer to color definitions in a ResourceDictionary
that look like this:
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">#33302E</Color>
<Color x:Key="textColor">White</Color>
</ResourceDictionary>
</Application.Resources>
That seems to be old news, however, because if you try that now, you get a compilation error -- The type 'Color' does not support direct content
.
The current norm seems to be to use a FactoryMethod
(like FromHsla
) and pass numeric color arguments. But is there a current concise syntax for defining Color
keys (by name, ideally, like Red
) in a ResourceDictionary
, using XAML?
xaml xamarin.forms
xaml xamarin.forms
asked Nov 24 '18 at 0:38
Graham CharlesGraham Charles
5,64632035
5,64632035
The way you're doing it should work perfectly well. Make sure you're referencing all necessaryxmlns
namespaces.
– jsanalytics
Nov 24 '18 at 22:52
It demonstrably does not work in Xamarin.Forms 3.4 (latest), your jpeg notwithstanding. The only namespace that's referenced is xmlns="xamarin.com/schemas/2014/forms" -- what else are you suggesting is required? I tried referencing System.Color, but the same compilation error comes up -- does not support direct content.
– Graham Charles
Nov 25 '18 at 4:50
Are you missing thehttp://
in your namespace ?
– jsanalytics
Nov 25 '18 at 7:41
No. Here's the entire definition:<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:assets="clr-namespace:MyApp.Assets" x:Class="MyApp.Assets.Colors">
It's not my references -- the syntax simply doesn't work anymore in 3.4. If <Color>Red</Color> still works for you, you're either using an older X.F or some other Color library.
– Graham Charles
Nov 25 '18 at 20:50
It works with both, 3.1 and 3.4.0.1008975. Sometimes VS gets a little cranky when it comes to XAML. One thing you can try is to tell it exactly where to findColor
:xmlns:xyz="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
and then use<xyz:Color/>
. Of course, adjust it for whatever prefix you like.
– jsanalytics
Nov 25 '18 at 22:06
|
show 1 more comment
The way you're doing it should work perfectly well. Make sure you're referencing all necessaryxmlns
namespaces.
– jsanalytics
Nov 24 '18 at 22:52
It demonstrably does not work in Xamarin.Forms 3.4 (latest), your jpeg notwithstanding. The only namespace that's referenced is xmlns="xamarin.com/schemas/2014/forms" -- what else are you suggesting is required? I tried referencing System.Color, but the same compilation error comes up -- does not support direct content.
– Graham Charles
Nov 25 '18 at 4:50
Are you missing thehttp://
in your namespace ?
– jsanalytics
Nov 25 '18 at 7:41
No. Here's the entire definition:<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:assets="clr-namespace:MyApp.Assets" x:Class="MyApp.Assets.Colors">
It's not my references -- the syntax simply doesn't work anymore in 3.4. If <Color>Red</Color> still works for you, you're either using an older X.F or some other Color library.
– Graham Charles
Nov 25 '18 at 20:50
It works with both, 3.1 and 3.4.0.1008975. Sometimes VS gets a little cranky when it comes to XAML. One thing you can try is to tell it exactly where to findColor
:xmlns:xyz="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
and then use<xyz:Color/>
. Of course, adjust it for whatever prefix you like.
– jsanalytics
Nov 25 '18 at 22:06
The way you're doing it should work perfectly well. Make sure you're referencing all necessary
xmlns
namespaces.– jsanalytics
Nov 24 '18 at 22:52
The way you're doing it should work perfectly well. Make sure you're referencing all necessary
xmlns
namespaces.– jsanalytics
Nov 24 '18 at 22:52
It demonstrably does not work in Xamarin.Forms 3.4 (latest), your jpeg notwithstanding. The only namespace that's referenced is xmlns="xamarin.com/schemas/2014/forms" -- what else are you suggesting is required? I tried referencing System.Color, but the same compilation error comes up -- does not support direct content.
– Graham Charles
Nov 25 '18 at 4:50
It demonstrably does not work in Xamarin.Forms 3.4 (latest), your jpeg notwithstanding. The only namespace that's referenced is xmlns="xamarin.com/schemas/2014/forms" -- what else are you suggesting is required? I tried referencing System.Color, but the same compilation error comes up -- does not support direct content.
– Graham Charles
Nov 25 '18 at 4:50
Are you missing the
http://
in your namespace ?– jsanalytics
Nov 25 '18 at 7:41
Are you missing the
http://
in your namespace ?– jsanalytics
Nov 25 '18 at 7:41
No. Here's the entire definition:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:assets="clr-namespace:MyApp.Assets" x:Class="MyApp.Assets.Colors">
It's not my references -- the syntax simply doesn't work anymore in 3.4. If <Color>Red</Color> still works for you, you're either using an older X.F or some other Color library.– Graham Charles
Nov 25 '18 at 20:50
No. Here's the entire definition:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:assets="clr-namespace:MyApp.Assets" x:Class="MyApp.Assets.Colors">
It's not my references -- the syntax simply doesn't work anymore in 3.4. If <Color>Red</Color> still works for you, you're either using an older X.F or some other Color library.– Graham Charles
Nov 25 '18 at 20:50
It works with both, 3.1 and 3.4.0.1008975. Sometimes VS gets a little cranky when it comes to XAML. One thing you can try is to tell it exactly where to find
Color
: xmlns:xyz="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
and then use <xyz:Color/>
. Of course, adjust it for whatever prefix you like.– jsanalytics
Nov 25 '18 at 22:06
It works with both, 3.1 and 3.4.0.1008975. Sometimes VS gets a little cranky when it comes to XAML. One thing you can try is to tell it exactly where to find
Color
: xmlns:xyz="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
and then use <xyz:Color/>
. Of course, adjust it for whatever prefix you like.– jsanalytics
Nov 25 '18 at 22:06
|
show 1 more comment
2 Answers
2
active
oldest
votes
You need to try "SolidColorBrush" Color cannot be directly set. Something like below..
<!--#region COLORS-->
<SolidColorBrush x:Key="SCBCement" Color="#FF3B6983"/>
<SolidColorBrush x:Key="SCBOrange" Color="#FFFFAC48"/>
<SolidColorBrush x:Key="SCBLightGrey" Color="#FF3C3C3C"/>
<SolidColorBrush x:Key="SCBLightBrown" Color="#FF933A3A"/>
<SolidColorBrush x:Key="SCBYes" Color="#FF10BD10"/>
<SolidColorBrush x:Key="SCBNo" Color="#FFBD2010"/>
<!--#endregion-->
Then use it like below:
<ListView ItemContainerStyle="{StaticResource StyleListViewItem}" Background="{x:Null}" BorderBrush="{x:Null}" ItemsSource="{Binding Value}" Foreground="{StaticResource SCBCement}">
There's no definition forSolidColorBrush
in Xamarin.Forms 3.4. It appears to be a WPF thing?
– Graham Charles
Nov 25 '18 at 4:55
add a comment |
If you define a color via ResourceDictionary
you only can declare it by Hex (with or without alpha channel), the enum color actually is defined via code on the Color class, you can create a color list this way
namespace YourNamespace
{
public static class Colors
{
public statis Color BackgroundColor = Color.FromHex("#33302E");
...
}
}
Yes, I'm aware that I can use C# to declare the color constants --public static Color ReverseTextBackground = Color.DarkGray;
-- but the question is how to use XAML, as the documentation appears to be incorrect.
– Graham Charles
Nov 25 '18 at 4:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454233%2fdefine-color-in-xamarin-forms-xaml-without-the-type-color-does-not-support-di%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to try "SolidColorBrush" Color cannot be directly set. Something like below..
<!--#region COLORS-->
<SolidColorBrush x:Key="SCBCement" Color="#FF3B6983"/>
<SolidColorBrush x:Key="SCBOrange" Color="#FFFFAC48"/>
<SolidColorBrush x:Key="SCBLightGrey" Color="#FF3C3C3C"/>
<SolidColorBrush x:Key="SCBLightBrown" Color="#FF933A3A"/>
<SolidColorBrush x:Key="SCBYes" Color="#FF10BD10"/>
<SolidColorBrush x:Key="SCBNo" Color="#FFBD2010"/>
<!--#endregion-->
Then use it like below:
<ListView ItemContainerStyle="{StaticResource StyleListViewItem}" Background="{x:Null}" BorderBrush="{x:Null}" ItemsSource="{Binding Value}" Foreground="{StaticResource SCBCement}">
There's no definition forSolidColorBrush
in Xamarin.Forms 3.4. It appears to be a WPF thing?
– Graham Charles
Nov 25 '18 at 4:55
add a comment |
You need to try "SolidColorBrush" Color cannot be directly set. Something like below..
<!--#region COLORS-->
<SolidColorBrush x:Key="SCBCement" Color="#FF3B6983"/>
<SolidColorBrush x:Key="SCBOrange" Color="#FFFFAC48"/>
<SolidColorBrush x:Key="SCBLightGrey" Color="#FF3C3C3C"/>
<SolidColorBrush x:Key="SCBLightBrown" Color="#FF933A3A"/>
<SolidColorBrush x:Key="SCBYes" Color="#FF10BD10"/>
<SolidColorBrush x:Key="SCBNo" Color="#FFBD2010"/>
<!--#endregion-->
Then use it like below:
<ListView ItemContainerStyle="{StaticResource StyleListViewItem}" Background="{x:Null}" BorderBrush="{x:Null}" ItemsSource="{Binding Value}" Foreground="{StaticResource SCBCement}">
There's no definition forSolidColorBrush
in Xamarin.Forms 3.4. It appears to be a WPF thing?
– Graham Charles
Nov 25 '18 at 4:55
add a comment |
You need to try "SolidColorBrush" Color cannot be directly set. Something like below..
<!--#region COLORS-->
<SolidColorBrush x:Key="SCBCement" Color="#FF3B6983"/>
<SolidColorBrush x:Key="SCBOrange" Color="#FFFFAC48"/>
<SolidColorBrush x:Key="SCBLightGrey" Color="#FF3C3C3C"/>
<SolidColorBrush x:Key="SCBLightBrown" Color="#FF933A3A"/>
<SolidColorBrush x:Key="SCBYes" Color="#FF10BD10"/>
<SolidColorBrush x:Key="SCBNo" Color="#FFBD2010"/>
<!--#endregion-->
Then use it like below:
<ListView ItemContainerStyle="{StaticResource StyleListViewItem}" Background="{x:Null}" BorderBrush="{x:Null}" ItemsSource="{Binding Value}" Foreground="{StaticResource SCBCement}">
You need to try "SolidColorBrush" Color cannot be directly set. Something like below..
<!--#region COLORS-->
<SolidColorBrush x:Key="SCBCement" Color="#FF3B6983"/>
<SolidColorBrush x:Key="SCBOrange" Color="#FFFFAC48"/>
<SolidColorBrush x:Key="SCBLightGrey" Color="#FF3C3C3C"/>
<SolidColorBrush x:Key="SCBLightBrown" Color="#FF933A3A"/>
<SolidColorBrush x:Key="SCBYes" Color="#FF10BD10"/>
<SolidColorBrush x:Key="SCBNo" Color="#FFBD2010"/>
<!--#endregion-->
Then use it like below:
<ListView ItemContainerStyle="{StaticResource StyleListViewItem}" Background="{x:Null}" BorderBrush="{x:Null}" ItemsSource="{Binding Value}" Foreground="{StaticResource SCBCement}">
answered Nov 24 '18 at 11:11
Senguttuvan MahalingamSenguttuvan Mahalingam
744
744
There's no definition forSolidColorBrush
in Xamarin.Forms 3.4. It appears to be a WPF thing?
– Graham Charles
Nov 25 '18 at 4:55
add a comment |
There's no definition forSolidColorBrush
in Xamarin.Forms 3.4. It appears to be a WPF thing?
– Graham Charles
Nov 25 '18 at 4:55
There's no definition for
SolidColorBrush
in Xamarin.Forms 3.4. It appears to be a WPF thing?– Graham Charles
Nov 25 '18 at 4:55
There's no definition for
SolidColorBrush
in Xamarin.Forms 3.4. It appears to be a WPF thing?– Graham Charles
Nov 25 '18 at 4:55
add a comment |
If you define a color via ResourceDictionary
you only can declare it by Hex (with or without alpha channel), the enum color actually is defined via code on the Color class, you can create a color list this way
namespace YourNamespace
{
public static class Colors
{
public statis Color BackgroundColor = Color.FromHex("#33302E");
...
}
}
Yes, I'm aware that I can use C# to declare the color constants --public static Color ReverseTextBackground = Color.DarkGray;
-- but the question is how to use XAML, as the documentation appears to be incorrect.
– Graham Charles
Nov 25 '18 at 4:52
add a comment |
If you define a color via ResourceDictionary
you only can declare it by Hex (with or without alpha channel), the enum color actually is defined via code on the Color class, you can create a color list this way
namespace YourNamespace
{
public static class Colors
{
public statis Color BackgroundColor = Color.FromHex("#33302E");
...
}
}
Yes, I'm aware that I can use C# to declare the color constants --public static Color ReverseTextBackground = Color.DarkGray;
-- but the question is how to use XAML, as the documentation appears to be incorrect.
– Graham Charles
Nov 25 '18 at 4:52
add a comment |
If you define a color via ResourceDictionary
you only can declare it by Hex (with or without alpha channel), the enum color actually is defined via code on the Color class, you can create a color list this way
namespace YourNamespace
{
public static class Colors
{
public statis Color BackgroundColor = Color.FromHex("#33302E");
...
}
}
If you define a color via ResourceDictionary
you only can declare it by Hex (with or without alpha channel), the enum color actually is defined via code on the Color class, you can create a color list this way
namespace YourNamespace
{
public static class Colors
{
public statis Color BackgroundColor = Color.FromHex("#33302E");
...
}
}
answered Nov 24 '18 at 20:21
FabriBertaniFabriBertani
363411
363411
Yes, I'm aware that I can use C# to declare the color constants --public static Color ReverseTextBackground = Color.DarkGray;
-- but the question is how to use XAML, as the documentation appears to be incorrect.
– Graham Charles
Nov 25 '18 at 4:52
add a comment |
Yes, I'm aware that I can use C# to declare the color constants --public static Color ReverseTextBackground = Color.DarkGray;
-- but the question is how to use XAML, as the documentation appears to be incorrect.
– Graham Charles
Nov 25 '18 at 4:52
Yes, I'm aware that I can use C# to declare the color constants --
public static Color ReverseTextBackground = Color.DarkGray;
-- but the question is how to use XAML, as the documentation appears to be incorrect.– Graham Charles
Nov 25 '18 at 4:52
Yes, I'm aware that I can use C# to declare the color constants --
public static Color ReverseTextBackground = Color.DarkGray;
-- but the question is how to use XAML, as the documentation appears to be incorrect.– Graham Charles
Nov 25 '18 at 4:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454233%2fdefine-color-in-xamarin-forms-xaml-without-the-type-color-does-not-support-di%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The way you're doing it should work perfectly well. Make sure you're referencing all necessary
xmlns
namespaces.– jsanalytics
Nov 24 '18 at 22:52
It demonstrably does not work in Xamarin.Forms 3.4 (latest), your jpeg notwithstanding. The only namespace that's referenced is xmlns="xamarin.com/schemas/2014/forms" -- what else are you suggesting is required? I tried referencing System.Color, but the same compilation error comes up -- does not support direct content.
– Graham Charles
Nov 25 '18 at 4:50
Are you missing the
http://
in your namespace ?– jsanalytics
Nov 25 '18 at 7:41
No. Here's the entire definition:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:assets="clr-namespace:MyApp.Assets" x:Class="MyApp.Assets.Colors">
It's not my references -- the syntax simply doesn't work anymore in 3.4. If <Color>Red</Color> still works for you, you're either using an older X.F or some other Color library.– Graham Charles
Nov 25 '18 at 20:50
It works with both, 3.1 and 3.4.0.1008975. Sometimes VS gets a little cranky when it comes to XAML. One thing you can try is to tell it exactly where to find
Color
:xmlns:xyz="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
and then use<xyz:Color/>
. Of course, adjust it for whatever prefix you like.– jsanalytics
Nov 25 '18 at 22:06