2017-08-12 73 views
1

我想創建一個自定義的TabbedView,但是當我添加一些CustomTab槽xaml頁面時,可綁定的屬性不會改變。BindableProperty列表不會改變

下面的代碼有什麼錯誤?

在此先感謝。

public static readonly BindableProperty TabsProperty = BindableProperty.Create("Tabs", typeof(List<CustomTab>), typeof(TabbedView), new List<CustomTab>(), BindingMode.Default, null, TabsPropertyChanged, TabsPropertyChanging); 

public List<CustomTab> Tabs 
{ 
    get 
    { 
     return (List<CustomTab>)GetValue(TabsProperty); 
    } 
    set 
    { 
     SetValue(TabsProperty, value); 
     OnPropertyChanged("Tabs"); 
    } 
} 

private static void TabsPropertyChanging(BindableObject bindable, object oldValue, object newValue) 
{ 
} 

private static void TabsPropertyChanged(BindableObject bindable, object oldValue, object newValue) 
{ 
} 

XAML

<pageComponents:TabbedView x:Name="myTabs"> 
    <pageComponents:TabbedView.Tabs> 
     <pageComponents:CustomTab></pageComponents:CustomTab> 
     <pageComponents:CustomTab></pageComponents:CustomTab> 
     <pageComponents:CustomTab></pageComponents:CustomTab> 
    </pageComponents:TabbedView.Tabs> 
</pageComponents:TabbedView> 

回答

0

兩件事情:一是 - 你不想竟設置列表中,但只能增加它在XAML,所以只是做一個getter和初始化。 其次 - 列表需要被觀察到,以便在添加或刪除某些內容時更新視圖。

這應該做你想要什麼:

public IList<CustomTab> Tabs { get; } = new ObservableCollection<CustomTab>(); 
0

試試這個:

您BindableProperty定義更改爲這樣的事情:

public static readonly BindableProperty TabsProperty = BindableProperty.Create(
    nameof(Tabs), 
    typeof(IEnumerable), 
    typeof(TabbedView)); 

粘土表示你不想讓你的收集實例將從外部設置,因此請將您的屬性更改爲類似於:

public ObservableCollection<CustomTab> Tabs { get; } = new ObservableCollection<CustomTab>(); 

在您的構造函數中訂閱CollectionChanged並使用該方法在集合更改(添加新項目)時收到通知。

public TabbedView() 
{ 
    Tabs.CollectionChanged += OnTabsChanged; 
} 

事件處理程序

private void OnTabsChanged(object sender, NotifyCollectionChangedEventArgs e) 
{ 
    // Do here what you need to do 
} 

提示:

如果設置ContentPropertyTabs屬性,您可以添加在XAML標籤無需指定標籤。

[ContentProperty(nameof(Tabs))] 
public partial class TabbedView : ContentView 
{ 
    ... 
} 

然後在你的XAML這就足夠

<pageComponents:TabbedView> 
     <pageComponents:CustomTab /> 
     <pageComponents:CustomTab /> 
     <pageComponents:CustomTab /> 
</pageComponents:TabbedView> 

希望這helps.-