Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Padding in StaticResource

I want to build few pages with specific Padding in sytacklayout. I do not want to wrote it in every pages. And its not good design considering if I want to change it in future. Is there a way to define padding in static resource? So that I can define it in one place App.xml and use it in other pages.

I have tried

<ContentPage.Resources>
    <ResourceDictionary>
        <x:String x:Key="padding">45, 0, 45, 0</x:String>
    </ResourceDictionary>
</ContentPage.Resources>
<StackLayout Padding="{StaticResource padding}">
    <Label Text="{StaticResource padding}"/>
</StackLayout>
like image 848
atiq1589 Avatar asked Oct 18 '25 14:10

atiq1589


2 Answers

As said, you should use Thickness. In addition, for setting your resource at the Application level, put it in your App.xaml file:

<Application ...>
 <Application.Resources>
        <ResourceDictionary>
            <Thickness x:Key="padding">45,0, 45, 0</Thickness>
            // some others resources...
        </ResourceDictionary>
    </Application.Resources>
</Application>

Then call it from your different pages:

<ContentPage>
    <StackLayout Padding="{StaticResource padding}">
        <Label Text="{StaticResource padding}"/>
    </StackLayout>
    ...

link to documentation

Hope it helps!

like image 97
TaiT's Avatar answered Oct 20 '25 23:10

TaiT's


Just use Thickness instead of a string:

<ContentPage.Resources>
<ResourceDictionary>
    <Thickness x:Key="padding">45,0</Thickness>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Padding="{StaticResource padding}">
    <Label Text="{StaticResource padding}"/>
</StackLayout>
like image 28
hvaughan3 Avatar answered Oct 20 '25 22:10

hvaughan3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!