Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

StaticResource into MarkupExtension

$
0
0

I'm currently trying to use a static resource with an extension for my entry's FontSize property. I have this code piece of code:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="PROJECT.Sources.Pages.Extras.EditProfilePage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:control="clr-namespace:PROJECT.Sources.Controls;assembly=PROJECT"
    xmlns:extension="clr-namespace:PROJECT.Sources.Extensions;assembly=PROJECT"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <ContentPage.Resources>
        <ResourceDictionary>
            <Color x:Key="NL_BlueNight">#0E1728</Color>
            <Color x:Key="NL_OrangeBeer">#E87E07</Color>
            <Color x:Key="NL_OrangeSky">#BD4327</Color>
            <Color x:Key="NL_White">#ECECEC</Color>
            <sys:Double x:Key="EntryFontSize">20</sys:Double>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>
        <AbsoluteLayout BackgroundColor="{x:StaticResource NL_BlueNight}">

            <!-- Look at this part -->

            <control:CustomEntry
                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                AbsoluteLayout.Flags="All"
                ...
                FontSize="{extention:FontSize {x:StaticResource EntryFontSize}}"
                .../>

            <!-- Look at this part -->

        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

Where extension:FontSize is coming from :

[ContentProperty("FontSize")]
public class FontSizeExtension : IMarkupExtension
{
    public double FontSize { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return Services.Sizing.FontSizeAdapter(FontSize);
    }
}

Sizing.cs

public class Sizing
{
    public static double FontSizeAdapter(double fontSize)
    {
        switch (Device.RuntimePlatform)
        {
            case "Android":
                return (fontSize / 2);
            case "iOS":
                return fontSize;
            case "Windows":
            case "WinPhone":
                return fontSize;
            default:
                return fontSize;
        }
    }
}

However, when I do FontSize="{extention:FontSize {x:StaticResource EntryFontSize}}" it throws an exception that says that the value cannot be null.. How can I use the both at the same time? I mean the x:StaticResource and the Extension

Thank !


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>