// Interface
public interface IItem
{
Collection Collection
{
get;
set;
}
}
public class Collection : BindableObject
{
public static readonly BindableProperty ItemsProperty =
BindableProperty.Create("Items", typeof(ObservableCollection<People>), typeof(Collection), new ObservableCollection<People>(), BindingMode.TwoWay, null, null);
public ObservableCollection<People> Items
{
get { return (ObservableCollection<People>)GetValue(ItemsProperty); }
set { this.SetValue(ItemsProperty, value); }
}
}
//Model class.
public class People
{
public string Name
{
get;
set;
}
}
//Core logic implementation.
public class SourceItems :BindableObject, IItem
{
public static readonly BindableProperty CollectionProperty =
BindableProperty.Create("Collection", typeof(Collection), typeof(SourceItems), new Collection(), BindingMode.TwoWay, null, null);
public Collection Collection
{
get { return (Collection)GetValue(CollectionProperty); }
set { this.SetValue(CollectionProperty, value); }
}
}
Public class MainPage()
{
var firstsource = new SourceItems();
firstsource.Collection.Items.Add(new People() { Name= "People1" });
firstsource.Collection.Items.Add(new People() { Name = "People2" });
firstsource.Collection.Items.Add(new People() { Name = "People3" });
var secondSource = new SourceItems();
secondSource.Collection.Items.Add(new People() { Name = "People4" });
secondSource.Collection.Items.Add(new People() { Name = "People5" });
secondSource.Collection.Items.Add(new People() { Name = "People6" });
}
在这里,我在执行上述代码实例后为类“SourceItems”创建了两个不同的对象,firstsource.Collection.Items 包含 6 个项目,而 secondSource.Collection.Items 也包含相同的 6 个项目。它应该是 3。
请澄清我做错了什么。
我在调试时显示了两个实例值。