我开始使用Xamarin,但我似乎无法将数据绑定挂钩。基本上我有一个按钮,RotationProperty设置为滑块ValueProperty的绑定,但按钮也有一个旋转90度的点击事件。绑定工作正常,click事件也是如此,但当我单击按钮进行旋转时,它似乎打破了RotationProperty按钮上的绑定。这是一个错误还是我做错了什么? (我要猜测后者)。
Binding binding = new Binding();
public Anim()
{
InitializeComponent();
binding.Source = sldr0;
binding.Path = "Value";
btn0.SetBinding(RotationProperty, binding);
}
private void OnBtnClick(object sender, EventArgs e)
{
btn0.RemoveBinding(RotationProperty);
var btn = (Button)sender;
var change = btn.Rotation + 90;
if (change > 360)
{
change = change - 360;
}
sldr0.Value = change;
btn.RotateTo(change, 250U, Easing.SpringOut);
btn0.SetBinding(RotationProperty, binding);
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.Views.Anim">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="btn0"
Grid.Row="0"
Text="Button"
VerticalOptions="Center"
HorizontalOptions="FillAndExpand"
Clicked="OnBtnClick"/>
<Slider x:Name="sldr0"
Grid.Row="1"
Value="0"
Minimum="0"
Maximum="360"
VerticalOptions="Center"
HorizontalOptions="FillAndExpand"></Slider>
<Label Grid.Row="1"
Text="{Binding Source={x:Reference btn0}, Path=Rotation}>
</Label>
</Grid>
</ContentPage>