Unable to read double from application resource within the viewmodel class - xamarin

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;

Related

How to use an icon from an embedded resource font file in Xamarin Forms

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"

How can I add on-platform parameter into a Xamarin Label?

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"];

Keyboard overlapping the entry fields

i have a view which contains entry fields kept inside the scrollView,here when i tapped on entry field the keyboard popups and covers the remaining fields,by following this i couldnt able to solve my problem.i chnged my main activity as [Activity(Label = "MyApp", Icon = "#drawable/icon", Theme = "#style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, WindowSoftInputMode = SoftInput.AdjustResize)] and also checked for AdjustPan nothing worked,how to scroll those fields upto last field when keyboardpopups?
<ContentView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollView Orientation="Vertical">
<StackLayout Grid.Row="0" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" Padding="10">
<StackLayout.Spacing>
<OnIdiom x:TypeArguments ="x:Double" Phone = "8" Tablet ="16"/>
</StackLayout.Spacing>
<Entry x:Name="FirstNameEntry" PlaceholderColor="#9E9E9E" Text="{Binding FirstName}" Placeholder="First name" HorizontalOptions="FillAndExpand" TextColor="#191919" FontFamily="Avenir Book">
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Entry.HeightRequest>
</Entry>
<Entry x:Name="LastNameEntry" Text="{Binding LastName}" PlaceholderColor = "#9E9E9E" Placeholder="Last name" HorizontalOptions="FillAndExpand" TextColor="#191919" FontFamily="Avenir Book">
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Entry.HeightRequest>
</Entry>
<Entry x:Name="MobileNameEntry" Keyboard="Numeric" PlaceholderColor="#9E9E9E" Text="{Binding MobileNumber}" TextChanged = "On_PhoneNumberTextChanged" Placeholder="Mobile number" HorizontalOptions="FillAndExpand" TextColor="#191919" FontFamily="Avenir Book">
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Entry.HeightRequest>
</Entry>
<Entry x:Name="HouseNumberEntry" Text="{Binding HouseNumber}" PlaceholderColor="#9E9E9E" Placeholder="House number" HorizontalOptions="FillAndExpand" TextColor="#191919" FontFamily="Avenir Book">
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Entry.HeightRequest>
</Entry>
<Entry x:Name="StreetNameEntry" Text="{Binding StreetName}" PlaceholderColor="#9E9E9E" Placeholder="Street name" HorizontalOptions="FillAndExpand" TextColor="#191919" FontFamily="Avenir Book">
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Entry.HeightRequest>
</Entry>
<Entry x:Name="CityNameEntry" Text="{Binding City}" PlaceholderColor="#9E9E9E" Placeholder="City" HorizontalOptions="FillAndExpand" TextColor="#191919" FontFamily="Avenir Book">
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Entry.HeightRequest>
</Entry>
<Entry x:Name="PostCodeEntry" Text="{Binding PostalCode}" PlaceholderColor="#9E9E9E" Placeholder="Post code" HorizontalOptions="FillAndExpand" TextColor="#191919" FontFamily="Avenir Book">
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Entry.HeightRequest>
</Entry>
<Picker x:Name="AddressTypePicker" Title="Select type" HorizontalOptions="FillAndExpand" SelectedIndexChanged="AddressTypePicker_OnSelectedIndexChanged">
<Picker.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="40" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="45" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Picker.HeightRequest>
</Picker>
</StackLayout>
</ScrollView>
<StackLayout BackgroundColor="#f2c646" Grid.Row="1" Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="End" Padding="0">
<StackLayout.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="50" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="50" Android="60" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</StackLayout.HeightRequest>
<StackLayout Padding="10">
<Label Text="Cancel" TextColor="Black" HorizontalOptions="StartAndExpand" FontFamily="Avenir Book" VerticalTextAlignment="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="OnCancelBtnClicked"/>
</Label.GestureRecognizers>
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="15" Android="17" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="20" Android="22" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Label.FontSize>
</Label>
</StackLayout>
<StackLayout HorizontalOptions="EndAndExpand" Padding="10">
<Label Text="Save" TextColor="Black" VerticalTextAlignment="Center" FontFamily="Avenir Book">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding SaveCommand}"/>
</Label.GestureRecognizers>
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="15" Android="17" WinPhone="28" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="20" Android="22" WinPhone="28" />
</OnIdiom.Tablet>
</OnIdiom>
</Label.FontSize>
</Label>
</StackLayout>
</StackLayout>
</Grid>
I believe you have a situation of signup form. Why don't you use tableview with Intent="Form" as table view has a built-in scroller which can accommodate for keep-entry-in-view behaviour. It gives native look of a form as well.
I used it in one of my projects and it worked perfectly on both iOS and Android.
<TableView Intent="Form" HasUnevenRows="true">
</TableView>
Reference: TableView
Hope this helps

How to get selected item of Picker which is inside a ListView?

I am working on a listview in which I have a label and Picker.I want to detect the selected item of the picker which is inside the list view.I am not able to access the x:Name of picker in Xaml.cs file.Below is my code.Any one please help me if i am going in a wrong direction.Thanks in Advance.
Here is my Xaml code:
<ListView x:Name="MasterRoomList" BackgroundColor="Transparent"
ItemsSource="{Binding MasterRoomWindowsList}"
IsVisible="{Binding RoomsVisibility}" ItemTapped="OnItemTapped" SeparatorVisibility="None">
<ListView.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="150" Android="150" WinPhone="150" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="300" Android="300" WinPhone="300" />
</OnIdiom.Tablet>
</OnIdiom>
</ListView.HeightRequest>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding RoomName}" TextColor="Black" FontFamily="Avenir Book" VerticalTextAlignment="Center">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="13" Android="13" WinPhone="13" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="20" Android="20" WinPhone="20" />
</OnIdiom.Tablet>
</OnIdiom>
</Label.FontSize>
</Label>
<Picker x:Name="CurtainPicker" BackgroundColor="Transparent" TextColor="Black" ItemsSource="{Binding CurtainsTypeList}" SelectedIndexChanged="CurtainPicker_OnSelectedIndexChanged" Title="Select Style" Grid.Column="1" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
<Picker.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40" Android="40"/>
</OnIdiom.Tablet>
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="30"/>
</OnIdiom.Phone>
</OnIdiom>
</Picker.HeightRequest>
</Picker>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Here is my Xaml.cs Code:
private void CurtainPicker_OnSelectedIndexChanged(object sender, EventArgs e)
{
var selectedItem = (string) CurtainPicker.SelectedItem;
}
var picker = (Picker)sender;
var selectedItem = (string) picker.SelectedItem;

Xamarin Forms Fixed Size for an image

I'm trying to set the height and width of my image and have it a fixed size however if I shrink the container (frame, stackpanel, grid, etc...) the image shrinks along with it. However I would have assumed setting the height and width (WidthRequest, HeightRequest, MinimumHeightRequest, MinimumWidthRequest) would have forced the image to remain the set size... however it doesn't/
What can I do...
Try this; This will set the width and height of the image with the platform specific dimensions i.e. it will use dp for android and points for iOS.
<Image IsVisible="true">
<Image.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="72" Android="100" WinPhone="10" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="212" Android="140" WinPhone="10" />
</OnIdiom.Tablet>
</OnIdiom>
</Image.HeightRequest>
<Image.WidthRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="125" Android="150" WinPhone="10" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="265" Android="190" WinPhone="10" />
</OnIdiom.Tablet>
</OnIdiom>
</Image.WidthRequest>
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<OnPlatform.iOS>
<FileImageSource File="logo.png" />
</OnPlatform.iOS>
<OnPlatform.Android>
<FileImageSource File="logo.png" />
</OnPlatform.Android>
<OnPlatform.WinPhone>
<FileImageSource File="logo.png" />
</OnPlatform.WinPhone>
</OnPlatform>
</Image.Source>
</Image>

Resources