xamarin.forms以xaml设置厚度

丹尼尔·索塔(Daniel Soutar)

我有以下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>

它正在尝试-但失败-将iOS顶部的填充设置为20像素。完全简单,完全符合在线建议-不起作用。填充仍然明显为0-参见下文。我真的看不出问题是什么-这是XAML的一个完全合理的部分,在您提出之前,不,类似以下内容:

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

也不起作用。生成的代码:

// 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");
    }
}

图片:

在此处输入图片说明

有什么建议?对于它的价值,我知道我可以在C#中覆盖OnAppearance,但是我试图通过在XAML中保持外观和在C#中保持逻辑来遵循更好的做法,这不必要地增加了难度。我真的希望XAML得到更好的文档记录,这样的问题在编译时就被发现了,并且文档中的代码实际上按照它说的做:(

阿列克谢·斯特拉赫(Alexey Strakh)

您要处理的填充由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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Xamarin.Forms上的XAML中将BindingContext设置为ViewModel

xamarin.forms从xaml绑定到属性

更改Xamarin Forms XAML按钮的isVisible属性

如何设置TextColor Xamarin.Forms TableSection?

将图像绑定到URL Xamarin Forms XAML

Xamarin.Forms从mvvm ViewModel设置焦点

如何使用Xamarin.Forms中的XAML设置CachingStrategy的ListView子类化

Xamarin.Forms:有没有办法在xaml中以派生样式设置默认字体(家族)?

Xamarin.Forms.Xaml.XamlParseException

如何在Xamarin.Forms XAML中绘制路径

XAML中的Xamarin.Forms ListView AutoHeight

Xamarin.Forms长按效果-如何在隐藏代码中设置命令(NO XAML)

Xamarin.Forms,XAML,绑定和isVisible

Xamarin XAML:在代码中设置附加属性

Xamarin.Forms中的XAML绑定问题

在Xamarin.forms中无法在XAML页面上看到绑定

如何在XAML [Xamarin.Forms]中使用String以外的Type设置自定义属性值

Xamarin Forms在CollectionView中设置特定值

Xamarin.Forms,XAML和XMLNS问题

Xamarin.Forms ListView,如何设置RowHeight?

Xamarin Forms Switch XAML

Xamarin Forms - 从 Android 特定的 .cs 文件引用 XAML

Xamarin.Forms Segoe Icon Code Binding in XAML UWP project Xamarin Native Xaml

Xamarin Forms - 从自定义渲染器检查控件是否具有从 XAML 设置的属性

关于 Xamarin Forms - Xaml 编译

Xamarin.Forms jamesmontemagno 设置插件

Xamarin Forms,在棱镜中设置 BarBackgroundColor NavigationPage

Xamarin Forms (Xaml) ListView 不显示图像

Xamarin.Forms 中的设置列表