Having NullReferenceException when using Onplatform in XAML

<ContentPage.Resources>
<OnPlatform x:Key="RegularFontSize"
x:TypeArguments="NamedSize"
Android="Default"

                    iOS="Small" />
<Style x:Key="LabelStyle" TargetType="Label">
        <Setter Property="FontSize" Value="{StaticResource RegularFontSize}">

        </Setter>

</Style>
</ContentPage.Resources>

this is the code I am using to change the fontsize of a label according to the platform

Best Answers

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    1 - Please fix your post to include markdown so the site doesn't try to render your XAML. That way we can see all of your markup correctly. https://redpillxamarin.com/2016/12/13/faq-frequently-asked-questions/

    2 - You're using the old deprecated syntax for OnPlatform. Try updating your XAML to the current usagage and retest.
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/essential-xaml-syntax
    Example

        <ContentPage.Padding>
            <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>
        </ContentPage.Padding>
      ...
    </ContentPage>
    
  • tezzletezzle Member ✭✭
    edited September 2018
    <ContentPage.Resources>
                <Style x:Key="LabelStyle" TargetType="Label">
                    <Setter Property="FontSize" >
                        <Setter.Value>
                        <OnPlatform  x:TypeArguments="NamedSize">
                            <On Platform="iOS" Value="Small"/>
                        </OnPlatform>
                    </Setter.Value>
                    </Setter>
                </Style>
        </ContentPage.Resources>
    

    Now I am using this as you suggested but still getting the same exception.

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/essential-xaml-syntax

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    what if you use a fixed size instead of a named size? Or a static resource.
    Give this a try.
    If the color changes you know you're using the style and the sizes should therefore change as well.

                <OnPlatform x:Key="FontSizeBody"
                            x:TypeArguments="x:Double">
                    <On Platform="iOS"
                        Value="20.0" />
                    <On Platform="Android"
                        Value="24.0" />
                    <On Platform="UWP"
                        Value="30" />
                </OnPlatform>
    
              <Style x:Key="LableStyleBody">
                <Setter Property="FontSize" Value="{StaticResource FontSizeBody}" />
                <Setter Property="TextColor" Value="Lime" />
              </Style>
    
  • tezzletezzle Member ✭✭
    edited September 2018

    I tried this one

    <ContentPage.Resources>
            <OnPlatform x:Key="FontSizeBody"
                            x:TypeArguments="x:Double">
                <On Platform="iOS"
                        Value="15.0" />
                <On Platform="Android"
                        Value="24.0" />
    
            </OnPlatform>
    
            <Style x:Key="LableStyleBody" TargetType="Label">
                <Setter Property="FontSize" Value="{StaticResource FontSizeBody}" />
                <Setter Property="TextColor" Value="Lime" />
            </Style>
        </ContentPage.Resources>
    

    But still same exception !!!

    @ClintStLaurent

Sign In or Register to comment.