0
// 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。

请澄清我做错了什么。

图片

我在调试时显示了两个实例值。

4

3 回答 3

2

firstsource和在您的secondSource代码中不同,而firstsource.CollectionsecondSource.Collection.

因为当您对or执行set/getmethod( add new items) 时,它们都指向 ,它是一个静态属性,因此它在他的回答中提到的所有 SourceItems 实例中共享。firstsource.CollectionsecondSource.Collectionpublic static readonly BindableProperty CollectionPropertyJeppe Stig Nielsen

这就是为什么firstsource.Collection.Items拥有 6 个项目并且secondSource.Collection.Items还拥有相同的 6 个项目。它们是同一个集合。

于 2019-11-20T02:32:10.370 回答
0

您面临的问题是您使用new ObservableCollection<People>()默认值。这意味着每个实例都将使用相同的实例。new ObservableCollection<People>()是唯一执行的。

您可以为 Collection 属性分配一个新的 ObservableCollection

var firstsource = new SourceItems();
firstsource.Collection = new ObservableCollection<People>();

或者

public class Collection : BindableObject
{
    public static readonly BindableProperty ItemsProperty =
        BindableProperty.Create("Items", typeof(ObservableCollection<People>), typeof(Collection), null, BindingMode.TwoWay, null, null);
    public ObservableCollection<People> Items
    {
        get { return (ObservableCollection<People>)GetValue(ItemsProperty); }
        set { this.SetValue(ItemsProperty, value); }
    } = new ObservableCollection<People>();
} 

或者使用构造函数。

于 2019-11-19T21:57:42.527 回答
0

CollectionProperty是一个static字段,因此所有实例之间共享同一个SourceItems实例。

于 2019-11-19T22:09:38.457 回答