【发布时间】:2019-06-05 06:43:10
【问题描述】:
我有以下 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 有更好的文档记录,这样的问题在编译时就被发现了,并且文档中的代码实际上做了它所说的:(
【问题讨论】:
标签: c# xaml xamarin xamarin.forms