简体   繁体   English

xamarin.forms以xaml设置厚度

[英]xamarin.forms set thickness in xaml

I have the following XAML: 我有以下XAML:

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Foo"
    x:Class="Foo.MainPage">
<ContentPage.Padding>
    <OnPlatform x:TypeArguments="Thickness">
        <OnPlatform.iOS>0, 20, 0, 0</OnPlatform.iOS>
    </OnPlatform>
</ContentPage.Padding>
<StackLayout BindingContext="{x:Reference slider}"
             HorizontalOptions="Center">
    <BoxView Color="Green"
             Opacity="{Binding Value}" />
    <Label Text="{Binding Value,
           StringFormat='Value is {0:F2}' }"
           Opacity="{Binding Value }"/>
    <Slider x:Name="slider"/>
</StackLayout>

It is attempting - and failing - to set the padding to 20 pixels at the top on iOS. 它正在尝试-失败-将iOS顶部的填充设置为20像素。 Perfectly straightforward, exactly as suggested online - does not work. 完全简单,完全符合在线建议-不起作用。 The padding is still clearly 0 - see below. 填充仍然明显为0-参见下文。 I really cannot see what the issue would be - this is a completely reasonable bit of XAML, and before you ask, no, something like: 我真的看不到问题是什么-这是XAML的一个完全合理的部分,在您问之前,不,类似以下内容:

<ContentPage.Padding>
    <OnPlatform x:TypeArguments="Thickness">
        <On Platform="iOS" Value="0, 20, 0, 0" />
    </OnPlatform>
</ContentPage.Padding>

does not work either. 也不起作用。 The generated code: 生成的代码:

// MainPage.xaml.g.cs

[global::Xamarin.Forms.Xaml.XamlFilePathAttribute("MainPage.xaml")]
public partial class MainPage : global::Xamarin.Forms.ContentPage {

    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
    private global::Xamarin.Forms.Slider slider;

    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
    private void InitializeComponent() {
        global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(MainPage));
        slider = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.Slider>(this, "slider");
    }
}

Image: 图片:

在此处输入图片说明

Any suggestions? 有什么建议么? For what it's worth, I know I can override OnAppearance in C#, but I'm trying to follow better practice by keeping appearance in XAML and logic in C#, which this is making unnecessarily difficult. 对于它的价值,我知道我可以在C#中覆盖OnAppearance,但是我试图通过在XAML中保持外观和在C#中保持逻辑来遵循更好的做法,这不必要地增加了难度。 I really wish XAML were better documented, that issues like this were caught at compile-time, and the code in the docs actually did what it says it does :( 我真的希望XAML有更好的文档记录,这样的问题在编译时就可以发现,并且文档中的代码实际上按照其声明的方式进行了工作:(

The padding you are trying to handle is controlled by iOS + Xamarin.Forms, called Safe Area and you can control it by asking Xamarin.Forms to take this area into account: 您要处理的填充由iOS + Xamarin.Forms(称为安全区域)控制,您可以通过要求Xamarin.Forms考虑此区域来控制它:

xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true"

在此处输入图片说明

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Foo"
    x:Class="Foo.MainPage"
    xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
    ios:Page.UseSafeArea="true"
    BackgroundColor="Yellow">    
    <StackLayout BindingContext="{x:Reference slider}"
                 HorizontalOptions="Center"
                 BackgroundColor="Blue">
        <BoxView Color="Green"
                 Opacity="{Binding Value}"/>
        <Label Text="{Binding Value,
               StringFormat='Value is {0:F2}' }"
               Opacity="{Binding Value }"/>
        <Slider x:Name="slider"/>
    </StackLayout>
</ContentPage>

After some playing around, I've realised this is an actual bug: the problem goes away when you set the second value to anything other than 20. 经过一番尝试之后,我意识到这是一个实际的错误:将第二个值设置为20以外的任何值时,问题就消失了。

...why on Earth that is a bug, I will likely never know. ...为什么在地球是个虫子,我可能永远不会知道。 Anyone reading this, you are more than welcome to correct me/give a better answer. 任何阅读此书的人都非常欢迎您纠正我/给出更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM