/**********************/
代码演示
/**********************/
using Xamarin.Forms;
namespace momo
{
public partial class momoPage : ContentPage
{
public momoPage()
{
//InitializeComponent();
Label label = new Label {
Text = "Opacity Binding Demo",
FontSize = Device.GetNamedSize(NamedSize.Large,typeof(Label)),
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Center
};
Slider slider = new Slider {
VerticalOptions = LayoutOptions.CenterAndExpand
};
label.BindingContext = slider;
label.SetBinding(Label.OpacityProperty,"Value");
Padding = new Thickness(10, 0);
Content = new StackLayout {
Children = { label, slider }
};
}
}
}
/**********************/
XAML演示
/**********************/
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:momo"
x:Class="momo.momoPage"
Padding="10,0">
<StackLayout>
<Label Text="Opacity Binding Demo"
FontSize="Large" VerticalOptions="CenterAndExpand"
HorizontalOptions="Center"
BindingContext="{x:Reference Name=slider}"
Opacity="{Binding Path=Value}" />
<Slider x:Name="slider" VerticalOptions = "CenterAndExpand" />
</StackLayout>
</ContentPage>
using Xamarin.Forms;
namespace momo
{
public partial class momoPage : ContentPage
{
public momoPage()
{
InitializeComponent();
}
}
}
可以缩短标记。为Reference提供支持的公共类是ReferenceExtension,它定义了其内容属性为name。BindingExtension(不是公共类)的内容属性是Path,因此无需Name和Path参数和等号:
<Label Text="Opacity Binding Demo"
FontSize="Large" VerticalOptions="CenterAndExpand"
HorizontalOptions="Center"
BindingContext="{x:Reference slider}"
Opacity="{Binding Value}" />
来源:Microsoft_Press_eBook_CreatingMobileAppswithXamarinForms_PDF.pdf ---- Chapter 16 Data binding