簡體   English   中英

xamarin.forms以xaml設置厚度

[英]xamarin.forms set thickness in 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>

它正在嘗試-失敗-將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有更好的文檔記錄,這樣的問題在編譯時就可以發現,並且文檔中的代碼實際上按照其聲明的方式進行了工作:(

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

經過一番嘗試之后,我意識到這是一個實際的錯誤:將第二個值設置為20以外的任何值時,問題就消失了。

...為什么在地球是個蟲子,我可能永遠不會知道。 任何閱讀此書的人都非常歡迎您糾正我/給出更好的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM