簡體   English   中英

Xamarin 表單中的 BindableProperty (List<>)

[英]BindableProperty (List<>) in Xamarin Forms

我的問題如下:我的ContentPage有一個ResourceDictionary和一些DataTemplate s。 其中一個模板采用Horse對象(它包含List<Weight> )。 我顯示了該對象的幾個統計信息,但是當我想將變量從Horse對象傳遞到另一個視圖時,如下所示:

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

WeightOfHorse背后的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顯示它們。

我究竟做錯了什么?

請嘗試將 IList 更改為 ObservableCollection。

我想通了,這有效。 看來您需要通過更改事件來分配屬性。

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;
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM