如何仅在Xamarin Forms XAML中设置左页边距?

时间:2019-04-16 09:06:20

标签: c# xaml xamarin.forms

这个问题与How to set a top margin only in XAML?相同,只是关于Xamarin而不是WPF。

如何从XAML为视图设置单个边距?

链接的“问与答”表明默认实现始终将所有未指定的边距设置为0,即使是从代码开始也是如此。

3 个答案:

答案 0 :(得分:0)

解决方案是创建一个AttachedProperty

Bottom

以相同的方式对TopRight<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:utils="using:Sample.Utils" x:Class="Sample.MyContentPage"> <StackLayout Orientation="Vertical" utils:Margin.Left="{StaticResource InnerPageContentMarginLeft}"> </StackLayout> </ContentPage> 进行声明。然后像这样在XAML中使用它:

heroku config:set MY_FEATURE=1

资料来源:
* https://forums.xamarin.com/discussion/66026/use-attached-bindable-property-in-xaml
* https://stackoverflow.com/a/32408461/2550406

答案 1 :(得分:0)

除非我缺少任何内容,否则只需按以下方式指定保证金即可:

<Label
    Margin = "0,20,0,0"
    Text = "Hello World!" />

保证金指定为 left top right 底部值。

答案 2 :(得分:0)

也许尝试使用valueConverter,您可以在其中通过绑定将值发送到转换器并返回Thickness对象。 有点像

Margin ={{Binding Marginleft, Converter={StaticResource stringToThicknessConverter}}

您在其中传递字符串并返回厚度对象,例如   new thickness(0,marginleft,0,0)

您还可以直接绑定到viewModel中的厚度类型对象,但这是一个不好的做法,因为它会在ViewModel中创建一个View依赖项,从而破坏了MVVM的目的