Xamarin FontAwesome not working from code behind - xaml

I am wondering if I am missing some thing here.
When using FontAwesome in xaml for iOS it works just fine like this:
<Button Text="" HeightRequest="100" BackgroundColor="DarkRed" TextColor="White" FontSize="36">
<Button.FontFamily>
<OnPlatform x:TypeArguments="x:String" Android="fa-regular-400.ttf#Font Awesome 5 Free Regular" iOS="Font Awesome 5 Free" WinPhone="Assets/fa-regular-400.ttf#Font Awesome 5 Free" />
</Button.FontFamily>
</Button>
But when doing this in the code behind for the page I do not get the icon but its unicode 
Here is my code behind code:
var newBtn = new Button()
{
Text = "",
HeightRequest = 100,
BackgroundColor = Color.DarkRed,
TextColor = Color.White,
FontSize = 36
};
newBtn.FontFamily = Device.RuntimePlatform == Device.iOS ? "Font Awesome 5 Free" : null;

When using from within C# code, you have to use it like this:
Text = "\uf11a"

Related

Xamarin.Forms add ScrollView to ContentPage and wrap existing content

I have a Xamarin.Forms page written in .xaml On iOS platform only I am trying to wrap the content of the page in ScrollView to help fix resizing issue when keyboard is shown.
The page looks something like this:
<base:mypagebase...>
<ContentPage.Resources>
...
</ContentPage.Resources>
<ContentPage.Content>
<RelativeLayout x:Name="ViewContentLayout" VerticalOptions="FillAndExpand">
....
</RelativeLayou>
</ContentPage.Content>
</base:mypagebase>
I am trying in the constructor of my mpage.xaml.cs after InitializeComponent() to wrap my RelativeLayout in ScrollView
Something like this:
if (Device.RuntimePlatform == Device.iOS)
{
var scroll = new ScrollView();
scroll.Orientation = ScrollOrientation.Vertical;
scroll.VerticalOptions = LayoutOptions.FillAndExpand;
scroll.Content = ViewContentLayout;
Content = scroll;
}
It passes through but throws exception later:
Object reference not set to an instance of an object
at Xamarin.Forms.RelativeLayout.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00017] in RelativeLayout.cs:185
The order in which the properties are called matters, seems like the Content root had to be set first:
if (Device.RuntimePlatform == Device.iOS)
{
var viewContentLayout = ViewContentLayout;
var scroll = new ScrollView();
Content = scroll;
scroll.Content = viewContentLayout;
}

Xamarin Forms FontSize by Platform

There is a known issue with UWP Xamarin Apps in that the size that fonts render in for Windows Mobile (not sure about Desktop Windows 10) are MUCH larger than they render on the other two platforms iOS and Android. The fix I have found a for this is to put a numeric font size with a decimal point in it (for example 24.1 for Large font size) in UWP apps. This works great. What I would like not is to define a static resource in my App.xaml that will work for all platforms. What I tried, that obviously does not work, is the following:
<OnPlatform x:Key="CustLarge" x:TypeArguments="x:Double">
<On Platform="iOS|Android">Large</On>
<On Platform="UWP">24.1</On>
</OnPlatform>
The theory is that in my XAML I would simply code all my large font sizes as "CustLarge" like this:
FontSize = "{StaticResource CustLarge}" />
Any ideas how I can accomplish this without doing on OnPlatform for every FontSize in my app? Any help would be appreciated.
UPDATE: Below are screen shots of what I am talking about. The iOS emulator was for the iPhone 6 4.7" wide. The Windows 10 emulator was a the 10.0.15254.9 5" phone.
You can see the Map All text is way bigger than it should be. (I am doing a relative comparison to the text in the segment control to the right of the stand alone buttons.) In both cases the fontsize is set to MICRO.
So back to my question - does anyone know how to do this?
I was able to find a workaround by creating a custom ResourceDictionnary, and adding FontSizes in the constructor.
public class SResourceDictionnary : ResourceDictionary
{
public SResourceDictionnary()
{
Add("Micro", new OnPlatform<double>
{
Default = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
Platforms = { new On
{
Value = 12d,
Platform = new List<string>{"UWP"}
}
}
});
Add("Small", new OnPlatform<double>
{
Default = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
Platforms = { new On
{
Value = 14d,
Platform = new List<string>{"UWP"}
}
}
});
Add("Medium", new OnPlatform<double>
{
Default = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
Platforms = { new On
{
Value = 18d,
Platform = new List<string>{"UWP"}
}
}
});
Add("Large", new OnPlatform<double>
{
Default = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
Platforms = { new On
{
Value = 20d,
Platform = new List<string>{"UWP"}
}
}
});
}
Then I merged the dictionary in my App.xaml like this
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<helpers:SResourceDictionnary></helpers:SResourceDictionnary>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Label">
<Setter Property="FontSize" Value="{StaticResource Small}" ></Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
It allowed me to use FontSize as StaticResource.
The only disadvantage is that you lose intellisense in Xaml

Using Br tag in Xamarin Forms

I'm using Xamarin Forms. I use a BoxView for making spaces between lines but I don't know if I'm doing right. What is the proper way to using a br tag in Xamarin ? Is there an alternative way for it? I'm using this:
<StackLayout>
<Label Text="Line 1"></Label>
<BoxView HeightRequest="100"></BoxView>
<Label Text="Line 2"></Label>
</StackLayout>
Thank you for the replies, for a conclusion; I thought the simpliest alternative should be like this,
and normally, I'll use a Grid, for spacing.
<StackLayout>
<Label Text="10"></Label>
<StackLayout HeightRequest="100"></StackLayout>
<Label Text="20"></Label>
</StackLayout>
If you are already using a StackLayout, you can set 'spacing' between each element in the Stackayout, example :
<StackLayout Spacing="10">
Some other options can be found in the Xamarin Forum though... https://forums.xamarin.com/discussion/29700/label-and-n
you can follow the example bellow or use [GRID ][1]
StackLayout linesSTK= new StackLayout {
VerticalOptions = LayoutOptions.Center,
HorizontalOptions =LayoutOptions.CenterAndExpand,
Children = {
new Label { Text = "Line 1" },
new Label { Text = "Line 2" },
new Label { Text = "Line 3" }
}
};
There are multiple ways to do this depending on your requirements.
You can set the Margin property on your Label, also you can set the Spacing on your StackLayout which tells the StackLayout to put some space between the items in it.
Another way would be to change your StackLayout to a Grid. With this you have some more fine grained control because you can not only set the Spacing like on a StackLayout but you can also play with the row height and column widths. Within the Grid columns and rows you can nest a StackLayout for more control on how they should behave in there.
For more information on the Grid check the extensive guide in the Xamarin Documentation.

Adding Image to a Button in Windows 10 Phone App

I am trying to add an image to a button . I want to archive something like this
This is what I was trying to do but the image doesn't appear
<StackPanel Orientation = "Horizontal">
<Button x:Name = "kap2"
FontFamily = "Arial"
Content = "Te drejtat dhe lirite themelore"
Width = "250"
Height = "50"
HorizontalAllignment = "Left">
<Button.Background>
<ImageBrush ImageSource = "Asses/number/1.png" Stretch="Fill">
</ImageBrush>
</Button.Background>
</Button>
</StackPanel>
The word 'Asses' I think it should be 'Assets'.
If this doesn't work, try also
/Assets/number/1.png
The problem is about spelling. Make sure that folder names are correct and the image name is correct too.

Accessing "Background" attribute in XAML from VB

im quite new in vb programming..
i have this code snippet in my Form.xaml
<MenuItem Name="testItem" Background="Gray" Width="37" >
<MenuItem.Icon>
<Image Source="Image\test.png" Width="35" Height="35"/>
</MenuItem.Icon>
</MenuItem>
and in my Form.xaml.vb how do I access testItem's Background? I did something like
testItem.Background = Colors.Blue
but it doesnt work..
The Background property of the Menu expects a Brush, not a Color. You could use a SolidColorBrush like this:
testItem.Background = New SolidColorBrush(Colors.Red)