Previously I was able to do something like this
<OnPlatform x:TypeArguments="Color" Android="{StaticResource Primary}"/>
Now that, that syntax is deprecated, I'm trying to do this:
<OnPlatform x:TypeArguments="Color">
<On Platform="Android">{StaticResource Primary}</On>
</OnPlatform>
but I'm getting the following error:
Cannot convert "{StaticResource Primary}" into Xamarin.Forms.Color
How should my syntax be?
Because StaticResource is a markup extension, you can use it either through attribute usage, or element usage
For example, try this:
<OnPlatform x:TypeArguments="Color">
<On Platform="Android" Value="{StaticResource Primary}" />
</OnPlatform>
or,
<OnPlatform x:TypeArguments="Color">
<On Platform="Android">
<StaticResource Key="Primary" />
</On>
</OnPlatform>
Related
I have 2 styles in static resources. First ( with name LabelFont SetterProperty="FontSize")
<OnIdiom x:TypeArguments:"x:Double" Phone="15", Tablet "30"/>.
Second is for setting Margin with OnPlatform and is BasedOn="LabelFont" with name MarginLabel and SetterProperty="Margin":
<OnPlatform x:TypeArguments="x:Double">
<On Platform="Android" Value="20,0,0,0" />
</OnPlatform>
(Sorry I dont have whole code now, I can post later if its necessary...)
When I define a Label, where I use Style="{StaticResources MarginLabel}" the font is set, but not Margin. No error.
Can anybody help me where is the problem? Thank you.
Instead of creating two styles,you could combine them in oneļ¼
<Style x:Key="MarginLabel" TargetType="Label">
<Setter Property="FontSize">
<Setter.Value>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>15</OnIdiom.Phone>
<OnIdiom.Tablet>30</OnIdiom.Tablet>
</OnIdiom>
</Setter.Value>
</Setter>
<Setter Property="Margin">
<Setter.Value>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="Android" Value="20,0,0,0" />
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
then use like:
<Label Text="Hello" Style="{StaticResource MarginLabel}"></Label>
When using <Style TargetType="Label"> I cant get FontSize to work with NamedSize.
Using Xamarin.Forms version : 3.4.0.1008975
Working :
<Style TargetType="Label">
<Setter Property="FontSize" Value="Small" />
</Style>
NOT working :
<Style TargetType="Label">
<Setter Property="FontSize">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String"
Android="Small"
iOS="Medium"/>
</Setter.Value>
</Setter>
</Style>
I have tried to change the TypeArguments to <OnPlatform x:TypeArguments="NamedSize" .../> with no success.
I am using other propertysetters with <OnPlatform .../> and they work as they should. Its just FontSize.
<?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:App67"
x:Class="App67.MainPage">
<ContentPage.Resources>
<OnPlatform x:Key="textColor"
x:TypeArguments="Color">
<On Platform="WinPhone" Value="Red" />
<On Platform="Android" Value="Aqua" />
<On Platform="iOS" Value="#80FF80" />
</OnPlatform>
<OnPlatform x:Key="fontSize"
x:TypeArguments="Font">
<On Platform="WinPhone" Value="Bold,Large" />
<On Platform="Android" Value="Bold,Large" />
<On Platform="iOS" Value="Bold,Large" />
</OnPlatform>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Label Text="Check out my style." TextColor="{StaticResource textColor}" Font="{StaticResource fontSize}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
I'm trying to assign different CornerRadius on iOS and Android like following:
<Frame
HasShadow="false"
Padding="10"
BackgroundColor="Red">
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Double">
<On
Platform="iOS">20</On>
<On
Platform="Android">30</On>
</OnPlatform>
</Frame.CornerRadius>
<Label
Text="Hello World" />
</Frame>
But getting a
Cannot assign property "CornerRadius": Property does not exists, or is not assignable, or mismatching type between value and property
I've tried x:TypeArguments="Thickness" and x:TypeArguments="x:Int32". Decompiling the Assembly it appears CornerRadius is of type float. However, there is no Float property in x namespace, I mean x:TypeArguments="x:Float" doesn't exists.
Any ideas what I'm doing wrong or this is a bug?
The CornerRadius type is a Single:
<Frame HasShadow="true" OutlineColor="Red">
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Single">
<On Platform="iOS" Value="20"/>
<On Platform="Android" Value="30"/>
</OnPlatform>
</Frame.CornerRadius>
<Frame.Content>
<Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" />
</Frame.Content>
</Frame>
Well I fell there is a syntax error here do something like this :
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Single">
<OnPlatform.Platforms>
<On Platform="iOS" Value="20" />
<On Platform="Android" Value="30" />
<On Platform="UWP" Value="30" />
</OnPlatform.Platforms>
</OnPlatform>
</Frame.CornerRadius>
Try this and in case it doesn't work kindly revert.
I'm trying to have a effect on all my buttons, but I can't seem to get the configuration right.
this is what I have now:
<ResourceDictionary>
<ControlTemplate x:Key="ButtonTemplate" >
<Grid RowSpacing="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<ContentPresenter Grid.Row="0" Grid.Column="0" >
<ContentPresenter.Effects>
<effects:ShadowEffect Radius="5" DistanceX="5" DistanceY="5">
<effects:ShadowEffect.Color>
<OnPlatform x:TypeArguments="Color">
<On Platform="iOS" Value="Black" />
<On Platform="Android" Value="White" />
<On Platform="UWP" Value="Red" />
</OnPlatform>
</effects:ShadowEffect.Color>
</effects:ShadowEffect>
</ContentPresenter.Effects>
</ContentPresenter>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type Button}">
<Setter Property="ControlTemplate" Value="{StaticResource ButtonTemplate}"></Setter>
</Style>
</ResourceDictionary>
But this throws a Can't resolve ControlTemplateProperty on Button.
Anybody has any idea on how to do this?
As you can see at Xamarin.Forms Guides the Control Templates isn't applicable to Button.
A ControlTemplate can be applied to the following types by setting
their ControlTemplate properties:
ContentPage
ContentView
TemplatedPage
TemplatedView
The error Can't resolve ControlTemplateProperty on Button make sense. Button inherites from View, not TemplatedView as would be needed.
So about your main point, effects cann't consumed directly by style, but you can achieve this thru attached properties. Here is some good references to you:
How to create an effect
How to consume it with styles
A good example
I hope it help you (and sorry for my bad english)
I'm trying to style my UWP application with some specific styles, while on other platforms it should remain default.
This is my project layout:
I tried the following things:
In Clients.Shared creating a style as following:
<Style x:Key="SomeStyle" TargetType="Button" />
And adding the same Style key in the Clients.Themes.UWP so it could hopefully override it, but no luck.
Then I tried Having a Dummy Style and using the onPlatform but that didn't work either, but I still think this is the way to go. I had the following code:
<Style x:Key="DummyStyle" TargetType="Button">
<Setter Property="Style">
<Setter.Value>
<OnPlatform x:Key="ButtonStyle" x:TypeArguments="Style">
<On Platform="UWP" Value="{StaticResource SomeStyle}"></On>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
I tried messing around with merged ResourceDictionary.MergedDictionaries but there I couldn't include the xaml
Anybody have any clue?
Try this:
<OnPlatform x:TypeArguments="Color" Android="Green" iOS="White" WinPhone="White"
x:Key="PrimaryColor" />
<Style x:Key="ButtonColor" TargetType="Button">
<Setter Property="BackgroundColor" Value="{StaticResource PrimaryColor}" />
</Style>
<Button Text="Hello" HorizontalOptions="StartAndExpand"
Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource ButtonColor}" />
In this example, I am defining a style for a Button called ButtonColor. This Button will use a BackgroundColor of white for all platforms except Android where it will be Green.
I find this is the most common use for this tag with regard to styling; if you are working with fonts, be sure to run on simulator and actual devices to get the font that you want.
This syntax works for me:
<Style x:Key="zoneLabelStyle" TargetType="Label">
<Setter Property="HeightRequest" Value="18" />
<Setter Property="Margin">
<OnPlatform x:TypeArguments="Thickness">
<On Platform="Android" Value="0, -3, 0, 0" />
</OnPlatform>
</Setter>
</Style>
Why do not use
<Button.Style>
<OnPlatform x:TypeArguments="x:Style">
<OnPlatform.iOS>...</OnPlatform.iOS>
<OnPlatform.Android>...</OnPlatform.Android>
<OnPlatform.WinPhone>...</OnPlatform.WinPhone>
</OnPlatform>
</Button.Style>
Hope it will help.