属性 在 class 的新实例中和先前创建的相同 class 的实例引用相同的内存

Property in a new instance of class and previously created instance of same class are referenced same memory

// 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" });
}

这里我为 class 'SourceItems' 创建了两个不同的 object 在执行上面的代码实例后 firstsource.Collection.Items 包含 6 个项目并且 secondSource.Collection.Items 也包含相同的6 项。 应该是 3。

请澄清我做错了什么。

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

您遇到的问题是您使用 new ObservableCollection<People>() 作为默认值。这意味着每个实例都将使用相同的实例。 new ObservableCollection<People>()只是执行的

您可以为集合 属性 分配一个新的 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>();
} 

或者使用构造器。

您的 CollectionProperty 是一个 static 字段,因此所有 SourceItems 实例。

firstsourcesecondSource 在您的代码中不同,而 firstsource.CollectionsecondSource.Collection 相同 Collection。

因为当你对firstsource.CollectionsecondSource.Collection执行set/get方法(add new items)时,它们都指向public static readonly BindableProperty CollectionProperty,它是静态的属性 所以它在他的回答中提到的 Jeppe Stig Nielsen 所有 SourceItems 实例中共享。

这就是为什么 firstsource.Collection.Items 拥有 6 个项目而 secondSource.Collection.Items 也拥有相同的 6 个项目。他们是一样的Collection.