I'm trying to set the Padding based on the platform with the following code
<ContentView.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS">16,24,16,4</On>
<On Platform="Windows">16,16,16,4</On>
<On Platform="Android">16,16,16,4</On>
</OnPlatform>
</ContentView.Padding>
However, I'm getting a red squiggly on OnPlatform with the following error
Invalid type: expected type is BindableProperty, actual type is OnPlatform<Thickness>
What's the correct way to configure this?
This works fine on mine, also running 2.3.4. If it doesn't work on yours you will need to show more code, perhaps doing something weird in the code behind.
<ContentView.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="16,24,16,4"></On>
<On Platform="Windows" Value="16,16,16,4"></On>
<On Platform="Android" Value="16,16,16,4"></On>
</OnPlatform>
</ContentView.Padding>
`<ContentView>
<ContentView.Padding>
<OnPlatform x:TypeArguments="Thickness"
Android="16,16,16,4"
WinPhone="16,16,16,4"
iOS="16,24,16,4"/>
</ContentView.Padding>
<ContentView>`
This worked for me
Related
I want to use icons from Font Awesome in my Xamarin Forms application. I have followed several tutorials I found but I still can't get the icons to display. Here is a portion of the static class showing the class name and namespace:
namespace NGIC_XAML.Constants
{
public static class IconFontsFAProRegular
{
public const string GlassMartini = "\uf000";
public const string Music = "\uf001";
public const string Search = "\uf002";
public const string Heart = "\uf004";
}
}
Here is the ExportFont statement in my AssemblyInfo.cs file:
[assembly: ExportFont("Font-Awesome-5-Pro-Regular-400.otf", Alias = "FAProRegular")]
Here is the declaration of the namespace in my XAML file:
xmlns:local="clr-namespace:NGIC_XAML.Constants"
And finally, here is the tag where I want to use one of the font icons:
<Image
HeightRequest="44"
HorizontalOptions="Center"
VerticalOptions="Center"
WidthRequest="44">
<Image.Source>
<FontImageSource
FontFamily="FAProRegular"
Glyph="{x:Static local:IconFontsFAProRegular.Heart}"
Size="44"
Color="{StaticResource NGIC_Red}" />
</Image.Source>
</Image>
The output
Someone is bound to ask, so here are the packages installed:
I would sure like to know what I;m doing wrong! I also don't get intellisense when I enter the namespace name in the ImageSource.
You must set font family as static resource and reference it in your code. Otherwise it will not work.
<Image.Source>
<FontImageSource
FontFamily="{StaticResource FontAwesomeRegular}"
Glyph="{x:Static local:IconFontsFAProRegular.Heart}"
Size="44"
Color="{StaticResource NGIC_Red}" />
Example:
<OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String">
<On Platform="Android" Value="FontAwesome5Brands.otf#Regular" />
<On Platform="iOS" Value="FontAwesome5Brands-Regular" />
<On Platform="UWP" Value="/Assets/FontAwesome5Brands.otf#Font Awesome 5 Brands" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String">
<On Platform="Android" Value="FontAwesome5Solid.otf#Regular" />
<On Platform="iOS" Value="FontAwesome5Free-Solid" />
<On Platform="UWP" Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String">
<On Platform="Android" Value="FontAwesome5Regular.otf#Regular" />
<On Platform="iOS" Value="FontAwesome5Free-Regular" />
<On Platform="UWP" Value="/Assets/FontAwesome5Regular.otf#Font Awesome 5 Free" />
</OnPlatform>
When using C# code in a XAML file it is important to add the assembly, with this you can use your Font Icons without any problem.
xmlns:local="clr-namespace:NGIC_XAML.Constants;assembly=NGIC_XAML"
I'm trying to apply Font Awesome 5 Duotone font family to my Xamarin Forms app but when launching it, icon is not displaying as it should...
<OnPlatform x:Key="FontAwesomeFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="fa-light-300.ttf#Font Awesome 5 Pro" />
<On Platform="iOS" Value="Font Awesome 5 Pro" />
<On Platform="UWP" Value="/Assets/Fonts/fa-light-300.ttf#Font Awesome 5 Pro" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeDuotoneFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="fa-duotone-900.ttf#Font Awesome 5 Duotone" />
<On Platform="iOS" Value="Font Awesome 5 Pro Duotone Solid" />
<On Platform="UWP" Value="/Assets/Fonts/fa-duotone-900.ttf#Font Awesome 5 Duotone" />
</OnPlatform>
In the code, FontAwesome Light works as expected. Am I doing anything wrong?
I have defined a resource in App.xaml which am trying to read from a view model class.
Different versions which were tried
<OnPlatform x:Key="HandpickedPhone"
x:TypeArguments="x:Double"
Android="250"
iOS="240" />
<OnPlatform x:Key="HandpickedTablet"
x:TypeArguments="x:Double"
Android="375"
iOS="360" />
<OnIdiom x:Key="HandpickedHeight"
x:TypeArguments="x:Double"
Phone="{StaticResource HandpickedPhone}"
Tablet="{StaticResource HandpickedTablet}" />
<OnPlatform x:Key="HorizontalListHeight"
x:TypeArguments="x:Double">
<OnPlatform.Android>
<OnIdiom x:TypeArguments="x:Double"
Phone="250"
Tablet="375" />
</OnPlatform.Android>
<OnPlatform.iOS>
<OnIdiom x:TypeArguments="x:Double"
Phone="240"
Tablet="260" />
</OnPlatform.iOS>
</OnPlatform>
I am trying to read it and set the height request dynamically like,
double height = (double)App.Current.Resources["HorizontalListHeight"];
horizontalStack.HeightRequest = height;
But casting to double throws IllegalCastException, and at the same time, I am able to cast into the correct value from the watcher in VS.
You have to use Width or Height or Thickness in x:TypeArguments
Like this:-
<OnPlatform x:TypeArguments="Thickness">
<OnPlatform.Platforms>
<On Platform="iOS" Value="0, 20, 0, 0" />
<On Platform="Android" Value="0, 0, 0, 0" />
<On Platform="UWP" Value="0, 0, 0, 0" />
</OnPlatform.Platforms>
</OnPlatform>
First , check https://forums.xamarin.com/discussion/comment/354754/#Comment_354754.
Here height is not double , it is OnPlatform<double> , so modify your code as
var height = (OnPlatform<double>)App.Current.Resources["HorizontalListHeight"];
horizontalStack.HeightRequest = height;
However , it's not the recommended way , we often use Style in ResourceDictionaty.
<Style x:Name="HorizontalListHeight" TargetType="StackLayout">
<Setter Property="HeightRequest">
<Setter.Value>
<OnPlatform x:TypeArguments="x:Double">
<OnPlatform.Android>
<OnIdiom x:TypeArguments="x:Double" Phone="250" Tablet="375" />
</OnPlatform.Android>
<OnPlatform.iOS>
<OnIdiom x:TypeArguments="x:Double" Phone="240" Tablet="260" />
</OnPlatform.iOS>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
horizontalStack.Style = Application.Current.Resources["HorizontalListHeight"] as Style;
Here's the code I have:
<?xml version="1.0" encoding="utf-8"?>
<Label xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.MessageLabel"
FontSize="{DynamicResource MessageTextFontSize}"
TextColor="{DynamicResource FooterTextColor}" />
I would like to make it so that the Property="FontFamily" is set like this:
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="FontAwesome5ProLight" />
<On Platform="Android" Value="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight" />
</OnPlatform>
but how can I make this into a parameter?
Like this, you can try. The platform syntax has changed in Xamarin.Forms 3.2 (I guess),
<Label xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.MessageLabel"
FontSize="{DynamicResource MessageTextFontSize}"
TextColor="{DynamicResource FooterTextColor}">
<Label.FontFamily>
<OnPlatform
x:TypeArguments="x:String"
Android="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight"
iOS="FontAwesome5ProLight" />
</Label.FontFamily>
</Label>
but the old syntax(mentioned in the question) will work as well if you want to use that.
<Label xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.MessageLabel"
FontSize="{DynamicResource MessageTextFontSize}"
TextColor="{DynamicResource FooterTextColor}">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="FontAwesome5ProLight" />
<On Platform="Android" Value="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight" />
</OnPlatform>
</Label.FontFamily>
</Label>
I'm not sure I understood your question properly, but is that what you are looking for ?
for better use define font family in style and apply wherever you want
<OnPlatform x:TypeArguments="x:String" x:Key="MediumFont">
<On Platform="Android" Value="fonts/HKGrotesk_Medium.ttf#HK Grotesk" />
<On Platform="UWP" Value="/Assets/Fonts/HKGrotesk_Medium.ttf#HK Grotesk" />
<On Platform="iOS" Value="HKGrotesk-Medium" />
</OnPlatform>
Add This In App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Auction.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:TypeArguments="x:String"
Android="FontAwesome5ProLight.otf#FontAwesome5ProLight"
iOS="FontAwesome5ProLight"
WinPhone="20"
x:Key="FontFamilyName" />
<Style x:Key="labelStyle" TargetType="Label">
<Setter Property="FontFamily" Value="{DynamicResource FontFamilyName}"/>
<Setter Property="FontSize" Value="12" />
<Setter Property="VerticalTextAlignment" Value="Center"/>
<Setter Property="TextColor" Value="#000000"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Use in Any Page Of Your App
in Xaml
<Label x:Name="Register" Text="Registeration" HorizontalTextAlignment ="Center" FontSize="20" Style="{StaticResource labelStyle}">
<Label.GestureRecognizers >
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" ></TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
and cs
label.Style = (Style)Application.Current.Resources["labelStyle"];
I have the following C# code:
var footer = new StackLayout()
{ BackgroundColor = Device.OnPlatform(Color.FromRgb(225, 240, 251), Color.FromRgb(225, 240, 251), Color.Black),
};
How can I translate that into Xamarin Xaml, more importantly the Device Platform specifics for the FromRgb?
I have the following XAML so far... again, it's the FromRgb that's stumping me.
<StackLayout.BackgroundColor>
<Color>
<OnPlatform x:TypeArguments="Color"></OnPlatform>
</Color>
</StackLayout.BackgroundColor>
UPDATE 14/02/2015
I've just tried the answer below and I have the following but it's not updating the background colour for either iOS or Windows Phone. If I add the Background Color to the root StackLayout element it works...:
<StackLayout HorizontalOptions="FillAndExpand">
<StackLayout.BackgroundColor>
<Color>
<OnPlatform x:TypeArguments="Color">
<OnPlatform.WinPhone>#51C0D4</OnPlatform.WinPhone>
<OnPlatform.iOS>#51C0D4</OnPlatform.iOS>
</OnPlatform>
</Color>
</StackLayout.BackgroundColor>
<Label Text=""></Label>
<StackLayout Orientation="Horizontal" VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Button Text="Login"></Button>
<Button Text="Sign Up"></Button>
</StackLayout>
</StackLayout>
</StackLayout>
You're almost there. The default converter takes care of converting the color from either a named color (e.g. White, Red, etc.) or a hex color (e.g.: #FF0000).
<StackLayout.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<OnPlatform.iOS>#FF0000</OnPlatform.iOS>
<OnPlatform.Android>#00FF00</OnPlatform.Android>
</OnPlatform>
</StackLayout.BackgroundColor>
Newer version syntax of OnPlatform is slightly different
In Xaml:
<ResourceDictionary>
<OnPlatform x:Key="SwitchOnColor" x:TypeArguments="Color" >
<On Platform="iOS|Android" >#0000FF</On>
<On Platform="UWP">#FF0000</On>
</OnPlatform>
</ResourceDictionary>
In code:
switch (Device.RuntimePlatform)
{
case "iOS":
break;
case "Android":
break;
case "UWP":
break;
default:
break;
}
You can do this also:
<ContentView>
<OnPlatform x:TypeArguments="View">
<On Platform="iOS">
<Label Text="iOS" />
</On>
<On Platform="Android">
<Label Text="Android" />
</On>
</OnPlatform>
</ContentView>