由字符串组成的ObservableCollection不更新

时间:2018-05-21 02:02:44

标签: xamarin data-binding

好的,我试图将我的Android项目中的ObservableCollection链接到我的跨平台项目::

到目前为止我已经知道了......这是我的跨平台应用

        ObservableCollection<String> NewRef = DependencyService.Get<ISlateBluetoothItems>().test().testThing;
        NewRef.CollectionChanged += TestThing_CollectionChanged;
        listView.ItemsSource = NewRef;


    private void TestThing_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        listView.ItemsSource = DependencyService.Get<ISlateBluetoothItems>().test().testThing;
        Console.WriteLine("working");

    }

生产线&#34;工作&#34;即使我对我的应用程序的android部分的ObservableCollection进行了更改,也永远不会打印...

这是我用于DependencyService的界面:

using System.Collections.ObjectModel;

  namespace ThoughtCastRewrite.BluetoothX
{
    public interface ISlateBluetoothItems 
    {

        BluetoothItems test();


    }
}

这是我用来公开列表的类:

namespace ThoughtCastRewrite.BluetoothX
{
    public class BluetoothItems 
    {
        public ObservableCollection<String> testThing;




        public BluetoothItems()
        {
            testThing = new ObservableCollection<String>();

            testThing.Add("wtf?");


        }

        public void AddThis()
        {
            testThing.Add("ok");

        }

    }
}

这是我应用的Android部分,它实现了ISlateBluetoothItems接口  BluetoothItems bluetoothItems = new BluetoothItems();

然后我打电话给

bluetoothItems.AddThis();

但是&#34;确定&#34;没有添加到我的列表中!我没有收到CollectionChanged事件!什么交易家伙?这笔交易是什么?

1 个答案:

答案 0 :(得分:0)

您应该只将ObservableCollection指定为列表视图的源,而不是在每次更改后。对集合的更改将自动传播到列表视图。