【问题标题】:OneWayToSource Binding with C# in XamarinOneWayToSource 在 Xamarin 中与 C# 绑定
【发布时间】:2020-05-28 18:53:43
【问题描述】:

我创建了一个滑块,它使用 XAML 中的 OneWayToSource 绑定来旋转标签,以了解Binding Modes 的工作原理。

我使用 XAML 让它工作,但是当我尝试在 C# 中实现同样的事情时,滑块不会旋转标签。

XAML 方法(有效):

XAML:

<StackLayout x:Name="stackLayout">
    <Label x:Name="Label" Text="XAML" VerticalOptions="CenterAndExpand" HorizontalOptions="Center"></Label>
    <Slider BindingContext="{x:Reference Label}"  Maximum="360.0" Value="{Binding Path=Rotation, Mode=OneWayToSource}"></Slider>
</StackLayout>

代码背后:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

C# 方法(不起作用):

XAML:

<StackLayout x:Name="stackLayout">
</StackLayout>

代码背后:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        Slider rotationSlider = new Slider {Maximum = 360.0};
        Label label = new Label
        {
            Text = "Code",
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.Center
        };
        rotationSlider.SetBinding(RotationProperty, "Value", BindingMode.OneWayToSource);
        rotationSlider.BindingContext = label;

        stackLayout.Children.Add(label);
        stackLayout.Children.Add(rotationSlider);
    }
}

我在 C# 方法中做错了什么?

【问题讨论】:

  • rotationSlider.SetBinding(Slider.ValueProperty, "Rotation", BindingMode.OneWayToSource);
  • @Jason 谢谢!如果您将其作为回复发布,我将接受它作为答案。

标签: c# xamarin xamarin.forms


【解决方案1】:

你有它向后 - 你想将滑块的Value 绑定到标签的Rotation

rotationSlider.SetBinding(Slider.ValueProperty, "Rotation", BindingMode.OneWayToSource);

【讨论】:

    猜你喜欢
    • 2011-12-30
    • 1970-01-01
    • 2014-08-27
    • 2012-11-26
    • 2016-06-08
    • 1970-01-01
    • 2010-10-14
    • 2011-06-20
    • 2014-12-04
    相关资源
    最近更新 更多