[英]Set BindingContext to ViewModel in XAML on Xamarin.Forms
[英]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.