xaml resources

rallyerallye ✭✭USUniversity ✭✭
edited October 2015 in Xamarin University

I'm getting a Java.Lang.RuntimeException in the Visual Studio emulator with the following xaml code:

(see attachment)

side note: how do you enter xaml code in this box without it getting interpreted as part of this web page?

Answers

  • rene_ruppertrene_ruppert Xamurai DEXamarin Team, University, XamUProfessors Xamurai

    Probably the easiest solution is to had over to http://gist.github.com and create a "Gist" where you can paste your code and then share the link to it.

    In your example: what is {StaticResource LabelFont}?

    • What class and namespace is LabelFont in?
    • What is the type?

    Can you provide a complete self contained example we can run and try?

  • rallyerallye ✭✭ USUniversity ✭✭

    I've attached the app.cs, LoginPage.xaml and LoginPage.xaml.cs

  • rene_ruppertrene_ruppert Xamurai DEXamarin Team, University, XamUProfessors Xamurai

    You add a Font object into your resource dictionary but initialize it with a text size ("Large"):

    <Font x:Key="LabelFont">Large</Font>

    That's not possible. You can set properties like the FontFamily or FontSize.

    You are then using the Font object and assign it to the FontSize attribute of the label:

    <Label x:name="lblLogin" Text="Username" FontSize="{StaticResource LabelFont}" />

    The types don't match. FontSize is a double value.

    For details how to set font properties in XAML, please check the online documentation.

  • rallyerallye ✭✭ USUniversity ✭✭

    Ok, I changed Font to FontSize in the resources and got the error 'java.lang.reflect.InvocationTargetException'

  • rene_ruppertrene_ruppert Xamurai DEXamarin Team, University, XamUProfessors Xamurai

    @rallye not FontSize but double; the FontSize property's type is double. Either you assign a double value or something that can be converted to one by a type converter:

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Fonts.FontTest">
        <ContentPage.Resources>
            <ResourceDictionary>
                <x:Double x:Key="FontSizeNumeric">30</x:Double>
                <x:String x:Key="FontSizeNamed">Large</x:String>
            </ResourceDictionary>
        </ContentPage.Resources>
        <StackLayout>
    
            <!-- Hard coded numeric font size (double) -->
            <Label Text="Hello Big World!" FontSize="55" />
    
            <!-- Hard coded named font size (string gets converted to double via a type converter) -->
            <Label Text="Hello Micro World!" FontSize="Micro" />
    
            <!-- Numeric value from resources;  FontSize is a double -->
            <Label Text="Hello Numeric Resource World!" FontSize="{StaticResource FontSizeNumeric}" />
    
            <!-- String value (names font size) from resources. Type is string and gets converted by the type converter. -->
            <Label Text="Hello Named Resource World!" FontSize="{StaticResource FontSizeNamed}" />
        </StackLayout>
    </ContentPage>
    
Sign In or Register to comment.