静态资源中的 Xamarin 表单填充

ati*_*589 3 xamarin xamarin.forms

我想在 sytacklayout 中构建几个带有特定 Padding 的页面。我不想在每一页都写它。考虑到我将来是否要更改它,它的设计并不好。有没有办法在静态资源中定义填充?这样我就可以在一个地方定义它App.xml并在其他页面中使用它。

我试过了

<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>
Run Code Online (Sandbox Code Playgroud)

Tai*_*T's 6

如上所述,您应该使用Thickness. 此外,为了在应用程序级别设置您的资源,请将其放入您的App.xaml文件中:

<Application ...>
 <Application.Resources>
        <ResourceDictionary>
            <Thickness x:Key="padding">45,0, 45, 0</Thickness>
            // some others resources...
        </ResourceDictionary>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

然后从您的不同页面调用它:

<ContentPage>
    <StackLayout Padding="{StaticResource padding}">
        <Label Text="{StaticResource padding}"/>
    </StackLayout>
    ...
Run Code Online (Sandbox Code Playgroud)

文档链接

希望能帮助到你!