【问题标题】:Setter of BindableProperty of Integer never called from xaml (Xamarin.forms)从未从 xaml (Xamarin.forms) 调用 Integer 的 BindableProperty 的设置器
【发布时间】:2016-09-03 10:05:28
【问题描述】:

我正在使用 Xamarin.forms 制作应用程序。

我将 BindableProperty 设置为整数。 但它从未调用过,甚至没有从 xaml 设置。

<... SelectedIndex="{Binding myValue}">

是不是我做错了什么?

谢谢。

   public class CircleSegmentControl : StackLayout
        {
            public static readonly BindableProperty SegmentInitialIndexProperty = BindableProperty.Create("SelectedIndex", typeof(int), typeof(CircleSegmentControl), 0);
            public int SelectedIndex {
set{ 
Debug.WriteLine("setter"); 
SetValue(SegmentInitialIndexProperty, value); 
SelectIndex(value); 
}
get{
Debug.WriteLine("getter"); 
return (int)GetValue(SegmentInitialIndexProperty);
}
}
    ...
    }

【问题讨论】:

  • 不确定 Xamarin,但可能与 WPF 依赖属性存在相同的问题:您不能在 getset 的方法中调用除 GetValueSetValue 之外的任何内容属性包装器。要获得有关属性值更改的通知(调用您的 SelectIndex 方法),您应该注册一个 propertyChanged 委托和另一个重载 BindableProperty.Create
  • @Clemens 谢谢。你的答案是正确的!它在 propertyChanged 委托中调用。

标签: xaml xamarin binding xamarin.forms


【解决方案1】:

显然这同样适用于 WPF 依赖属性。您不能在属性包装器的 getset 方法中调用除 GetValueSetValue 之外的任何内容:

public int SelectedIndex
{
    get { return (int)GetValue(SelectedIndexProperty); }
    set { SetValue(SelectedIndexProperty, value); }
}

要获得有关属性值更改的通知(调用您的 SelectIndex 方法),您应该注册一个 propertyChanged 委托和另一个 BindableProperty.Create 重载。

【讨论】:

  • 谢谢克莱门茨。你的回答是对的。我遇到了另一个问题。这个 ValueChanged 委托应该是静态的,因为 Bindable 属性是静态的。所以我无法访问类实例。如何解决这个问题?
  • 对不起。我想到了。委托有一个“可绑定对象”作为实例的参数。
猜你喜欢
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
相关资源
最近更新 更多