【问题标题】:BindableProperty (List<>) in Xamarin FormsXamarin 表单中的 BindableProperty (List<>)
【发布时间】:2020-09-26 15:52:49
【问题描述】:

我的问题如下: 我有我的ContentPage,其中有一个ResourceDictionary 和一些 DataTemplates。其中一个模板采用Horse 对象(其中包含List&lt;Weight&gt;'s)。 我显示了该对象的几个统计信息,但是当我想将变量从 Horse 对象传递到另一个视图时,如下所示:

<graph:LineGraph x:Name="displayWeight" WeightOfHorse="{Binding Weight}"/>

WeightOfHorse 背后的代码:

public static readonly BindableProperty WeightOfHorseProperty = BindableProperty.Create(
    nameof(WeightOfHorse),
    typeof(IList<Weight>),
    typeof(LineGraph),
    defaultBindingMode: BindingMode.TwoWay);

public IList<Weight> WeightOfHorse
{
    get => (IList<Weight>)GetValue(WeightOfHorseProperty);
    set => SetValue(WeightOfHorseProperty, value);
}

我只是无法传递这些值,我知道它们是可用的,因为我可以将它们显示在一个简单的 Collectionview 中。

我做错了什么?

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms bindableproperty


    【解决方案1】:

    请尝试将 IList 更改为 ObservableCollection。

    【讨论】:

    • 感谢您的建议,这有什么关系?也许我不清楚,该属性从未设置过。所以它似乎永远不会传递这些值。
    【解决方案2】:

    我想通了,这行得通。看来您需要通过更改事件来分配属性。

    public static readonly BindableProperty WeightOfHorseProperty = BindableProperty.Create(
    nameof(WeightOfHorse),
    typeof(IEnumerable<Weight>),
    typeof(LineGraph),
    defaultBindingMode: BindingMode.TwoWay,
    propertyChanged: OnEventNameChanged);
    
    public IEnumerable<Weight> WeightOfHorse
    {
    get => (IEnumerable<Weight>)GetValue(WeightOfHorseProperty);
    set => SetValue(WeightOfHorseProperty, value);
    }
    
    static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
    {
    var control = (LineGraph)bindable;
    control.WeightOfHorse = (List<Weight>)newValue;
    ...
    }
    

    【讨论】:

    • 嗨,很高兴找到了解决方案!记得有空的时候标记一下,对其他有同样问题的人会有帮助。
    猜你喜欢
    • 2017-12-27
    • 2017-08-13
    • 2020-07-21
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多