Setting image in app.xaml depending on platform - xamarin.forms

Hi I have been trying to set icon in my application based on images in the app.xaml
I have tried the following and they do not work:
usage in my page Icon="{StaticResource MyImageKey}">
<ImageSource x:Key="MyImageKey" >
<OnPlatform x:TypeArguments="ImageSource"
iOS="MyImage.jpg"
Android=""/>
</ImageSource>
<OnPlatform x:Key="MyImageKey"
x:TypeArguments="ImageSource">
<On Platform="iOS">"MyImage.png"</On>
<On Platform="Android">""</On>
</OnPlatform>
can you point me in the right direction?
thanks

You need add style in App resource like below :
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="MyImageKey"
x:TypeArguments="ImageSource"
iOS="contact.png"
Android="contact.png"
WinPhone="contact.png" />
</ResourceDictionary>
</Application.Resources>
Now you can use like below :
<Image Source="{StaticResource MyImageKey}"
HorizontalOptions="Center"
VerticalOptions="Center" />

Related

Missing Symbols after resuming windows in Xamarin.forms app

I have a Multiplattform app in Xamarin.forms for iOS, Android and UWP.
I'm using materialdesignicons-webfont.ttf for all my symbols.
If the windows goes in standby mode and resume after that, all symbols are missing.
<?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:SymbolTest"
x:Class="SymbolTest.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<OnPlatform x:Key="MaterialFontFamily" x:TypeArguments="x:String">
<On Platform="UWP" Value="Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Label Text="" HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" >
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="UWP" Value="Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform>
</Label.FontFamily>
</Label>
<Button Grid.Column="1" x:Name="btnSearch" BackgroundColor="LightBlue" >
<Button.ImageSource>
<FontImageSource FontFamily="{StaticResource MaterialFontFamily}" Size="36" Glyph="" Color="SlateGray" />
</Button.ImageSource>
</Button>
</StackLayout>
</ContentPage>
The ButtonImage ist missing the Label.Text is not missing
Missing Symbols after resuming windows in Xamarin.forms app
I created code sample follow this tutorial, And it works well if I sleep the computer and resuming, please check the following code if you have missed some key procedure.
<Label Text="">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="UWP" Value="Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform>
</Label.FontFamily>
</Label>
Update
I could reproduce your issue, and it only occurs in reference StaticResource scenario, it looks a bug, please feel free post the bug report in Xamarin github, and I will report it to related team.

Xamarin forms: How to increase the fontsize of label having TextType="Html"?

I have a label in my app UI that takes HTML data. So I set TextType="Html" property for that label. In small-screen devices I need 16 and for big-screen devices I need 32 as it's font size. But no change in label font size because of the TextType="Html" property.
XAML Label Code
<Label
x:Name="message_label"
VerticalOptions="Start"
VerticalTextAlignment="Center"
TextType="Html"
TextColor="Black"
Margin="5"
FontSize="Medium">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="MouseMemoiresRegular" />
<On Platform="Android" Value="MouseMemoiresRegular.ttf#MouseMemoiresRegular" />
<On Platform="UWP" Value="Assets/Fonts/MouseMemoiresRegular.ttf#MouseMemoiresRegular" />
</OnPlatform>
</Label.FontFamily>
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>16</OnIdiom.Phone>
<OnIdiom.Tablet>32</OnIdiom.Tablet>
</OnIdiom>
</Label.FontSize>
</Label>
So how can I increase the font size in this case?
If you want to put html in label, and want to increase the fontsize of label, I suggest you can use Xam.Plugin.HtmlLabel.
Firstly, you can install Xam.Plugin.HtmlLabel in your project,
Then render this in different platform.
iOS: AppDelegate.cs
HtmlLabelRenderer.Initialize();
global::Xamarin.Forms.Forms.Init();
Android: MainActivity.cs
HtmlLabelRenderer.Initialize();
global::Xamarin.Forms.Forms.Init(this, bundle);
UWP: App.xaml.csenter code here
var rendererAssemblies = new[] { typeof(HtmlLabelRenderer).GetTypeInfo().Assembly };
Xamarin.Forms.Forms.Init(e, rendererAssemblies);
HtmlLabelRenderer.Initialize();
than use the HtmlLable like this:
<htmllabel:HtmlLabel FontSize="12" Text="<h1>Hello World!</h1><br/>SecondLine" />
<htmllabel:HtmlLabel FontSize="20" Text="<h1>Hello World!</h1><br/>SecondLine" />
You can change different fontsize in htmllabel.

Global styles for FontImageSource property

I have following button in my Xamarin Forms application
<Button
Grid.Column="1"
Grid.Row="0">
<Button.ImageSource>
<FontImageSource Glyph="" FontFamily="{StaticResource IconFont}" Color="{StaticResource BaseColor}"/>
</Button.ImageSource>
</Button>
where IconFont is:
<OnPlatform x:TypeArguments="x:String" x:Key="IconFont">
<On Platform="Android" Value="materialdesignicons-webfont.ttf#Material design" />
<On Platform="iOS" Value="Material Design Icons" />
</OnPlatform>
Everything is working fine. Now, because all of my icons are in materialdesignicons-webfont and I will never use different icons I decided to move entire styling to App.xaml. I would like to set default properties like that:
<Style TargetType="FontImageSource">
<Setter Property="FontFamily" Value="{StaticResource IconFont}" />
<Setter Property="Color" Value="{StaticResource BaseColor}" />
</Style>
And use button by passing glyph only.
<Button
Grid.Column="1"
Grid.Row="0">
<Button.ImageSource>
<FontImageSource Glyph=""/>
</Button.ImageSource>
</Button>
For some reasons Icon is not rendered without specifying FontFamily and Color directly in FontImageSource. Am I missing something?
Is there some way to call my button in simpler way? F.e.
<Button
Grid.Column="1"
Grid.Row="0"
MDGlyph="" />
Unfortunately it is not possible to style a FontImageSource currently. However, there is a github issue requesting this capability.
have you tried this:
<Style x:Key="ButtonWithFontImageSource" TargetType="Button">
<Setter Property="ImageSource">
<FontImageSource
FontFamily="{StaticResource FontIcons}"
Glyph="{DynamicResource IconText}">
</FontImageSource>
<Setter>
</Style>

How to add font icon in toolbar item in xamarin forms

I have using navigation page in my application. I have to set font icon to toolbar item. But it shows cross symbol because i have not set the font family. So anyone suggest me to set font icon in toolbar item is possible or not.
With Xamarin Forms 4.x (if I remember the version correctly) they have add a FontImageSource type that can be used on ToolbarItems. A few things you need to do...
Add the font files to your platform projects. On Android, add them in the Assets folder with build action set to AndroidAsset. On iOS, add them in the Resources folder with build action set to BundleResource. Also on iOS, edit your info.plist and add
<key>UIAppFonts</key>
<array>
<string>fontname.ttf</string>
<string>fontname2.ttf</string>
...
</array>
Now Xamarin is able to use the fonts.
I defined some application resources to easily load fonts:
<OnPlatform x:Key="FontAwesomeLightFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="fa-light-300.ttf#Font Awesome 5 Pro Light" />
<On Platform="iOS" Value="FontAwesome5Pro-Light" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeRegularFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="fa-regular-400.ttf#FontAwesome5ProRegular" />
<On Platform="iOS" Value="FontAwesome5ProRegular" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeSolidFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="fa-solid-900.ttf#FontAwesome5ProSolid" />
<On Platform="iOS" Value="FontAwesome5ProSolid" />
</OnPlatform>
That is sitting in a resource dictionary that is merged into App.xaml resources.
Now, when I want to use a font icon as a toolbar icon imagesource, I can do the following:
<Page.ToolbarItems>
<ToolbarItem Command="{Binding SomeCommand}">
<ToolbarItem.IconImageSource>
<FontImageSource
FontFamily="{StaticResource FontAwesomeSolidFontFamily}"
Glyph="{x:Static fonts:FontAwesomeIcon.Cog}"
Size="{StaticResource ToolbarIconImageSourceSize}" />
</ToolbarItem.IconImageSource>
</ToolbarItem>
</Page.ToolbarItems>
If you want a static class to define glyph icons as opposed to having to enter the unicode (my FontAwesomeIcon class used in glyph above), there is a great tool at https://andreinitescu.github.io/IconFont2Code/ that will generate a C# static class based on an uploaded font file. It ends up looking like
public static class FontAwesomeIcon
{
public const string Abacus = "\uf640";
public const string Ad = "\uf641";
public const string AddressBook = "\uf2b9";
public const string AddressCard = "\uf2bb";
public const string Adjust = "\uf042";
public const string AirFreshener = "\uf5d0";
...
}
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Command="{Binding ExportDocumentCommand}">
<ToolbarItem.IconImageSource>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Size="Subtitle"
Glyph="{x:Static fontawesome:FontAwesomeIcons.FileExport}" />
</ToolbarItem.IconImageSource>
</ToolbarItem>
</ContentPage.ToolbarItems>
in app.xaml
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
<On Platform="iOS" Value="FontAwesome5Free-Solid" />
<On Platform="Android" Value="Fonts/FontAwesome5Solid.otf#Regular" />
<On Platform="UWP" Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
</OnPlatform>
for work this line "Glyph="{x:Static fontawesome:FontAwesomeIcons.FileExport}""
you need add on page
xmlns:fontawesome="clr-namespace:YourProjectName.Styles"
and add class from GitHub
You can try to use FontAwesome
First,we need to go to website: https://fontawesome.com/ to get vector icons and social logos on website with Font Awesome.
After we download the font and put it in our app, we can use it flexibly.
Here is a demo you can check it.
The main code is as follows:
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"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="MyFontApp.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:TypeArguments="x:String"
x:Key="FontAwesomeBrands">
<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:TypeArguments="x:String"
x:Key="FontAwesomeSolid">
<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:TypeArguments="x:String"
x:Key="FontAwesomeRegular">
<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>
</ResourceDictionary>
</Application.Resources>
class FontAwesomeIcons
namespace MyFontApp.Utils
{
public static partial class FontAwesomeIcons
{
public const string FiveHundredPX = "\uf26e";
public const string Abacus = "\uf640";
public const string AccessibleIcon = "\uf368";
// other font icon codes
}
}
And we use like this:
<NavigationPage.TitleView >
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Label Text="{x:Static Utils:FontAwesomeIcons.Map}"
FontFamily="{StaticResource FontAwesomeSolid}"/>
<Button Text="{x:Static Utils:FontAwesomeIcons.Anchor}"
FontFamily="{StaticResource FontAwesomeSolid}"/>
</StackLayout>
</NavigationPage.TitleView>
The result is:
You can review the full demo here.
For more details, you can check: https://medium.com/#tsjdevapps/use-fontawesome-in-a-xamarin-forms-app-2edf25311db4

OnPlatform in NavigationPage using XAML

Given this original code, I'm trying to hide the icon on Android:
<NavigationPage Title="Page" Icon="page_icon.png">
<x:Arguments>
<views:MyPage />
</x:Arguments>
</NavigationPage>
I have attempted to use OnPlatform many different ways and cannot get it to work. Here are some things I have tried:
<NavigationPage Title="Page">
<NavigationPage.Icon>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>page_icon.png</OnPlatform.iOS>
<OnPlatform.Android>{x:Null}</OnPlatform.Android>
</OnPlatform>
</NavigationPage.Icon>
<x:Arguments>
<views:MyPage />
</x:Arguments>
</NavigationPage>
This produced the error:
The given key was not present in the dictionary.
I also tried this:
<NavigationPage Title="Rater">
<OnPlatform
x:Key="Icon"
x:TypeArguments="x:String"
iOS="tab_feed.png"
Android="{x:Null}"
/>
<x:Arguments>
<views:MyPage />
</x:Arguments>
</NavigationPage>
Which runs, but no icons show up on iOS.
(Posted on behalf of the OP).
With the help of Yuri S's example, I was able for create markup that worked as intended on iOS and Android. This is it.
<NavigationPage.Icon>
<OnPlatform x:Key="SwitchOnIcon" x:TypeArguments="FileImageSource">
<On Platform="iOS">tab_feed.png</On>
</OnPlatform>
</NavigationPage.Icon>
Not sure how you deal with icons, it may be a separate question but OnPlatform was redesigned. Here is how you suppose to use it now:
<OnPlatform x:Key="SwitchOnColor" x:TypeArguments="Color" >
<On Platform="iOS,Android" >#0000FF</On>
<On Platform="UWP">#FF0000</On>
</OnPlatform>

Resources