0

编辑:按照建议对 Observable 集合进行更改,但仍然不起作用:新代码如下所示:

    private ObservableCollection<bool> _placeTypes = new ObservableCollection<bool>();
    //private static bool[] _placeTypes { get; set; }
    public ObservableCollection<bool> placeTypes
    {
        get
        {
            return _placeTypes;
        }
        set
        {
            _placeTypes = value;
            placechange = true;
            RaisePropertyChanged("placeTypes");
        }
    }

Oki,我有一个带有这种结构的 bool 数组的对象:

    private bool[] _placeTypes { get; set; }
    public bool[] placeTypes
    {
        get
        {
            return _placeTypes;
        }
        set
        {
            _placeTypes = value;
            placechange = true;
            RaisePropertyChanged("placeTypes");
        }
    }

对象以这种方式正确绑定到某些复选框:

<CheckBox x:Name="_1_Cafe" IsChecked="{Binding Path=placeTypes[0], Mode=TwoWay}" Content="Cafe" FlowDirection="RightToLeft" Width="429" HorizontalContentAlignment="Stretch" />
<CheckBox x:Name="_2_Pub" IsChecked="{Binding placeTypes[1], Mode=TwoWay}" Content="Pub" FlowDirection="RightToLeft" Width="429"/>
<CheckBox x:Name="_3_Chiringuito" IsChecked="{Binding placeTypes[2], Mode=TwoWay}" Content="Chiringuito" FlowDirection="RightToLeft" Width="429"/>
<CheckBox x:Name="_4_BeachClub" IsChecked="{Binding placeTypes[3], Mode=TwoWay}" Content="Beach Club" FlowDirection="RightToLeft" Width="429"/>
<CheckBox x:Name="_5_ClubDiscoteca" IsChecked="{Binding placeTypes[4], Mode=TwoWay}" Content="Club/Discoteca" FlowDirection="RightToLeft" Width="429"/>
<CheckBox x:Name="_6_Tetería" IsChecked="{Binding placeTypes[5], Mode=TwoWay}" Content="Tetería" FlowDirection="RightToLeft" Width="429"/>
<CheckBox x:Name="_7_Restaurante" IsChecked="{Binding placeTypes[6], Mode=TwoWay}" Content="Restaurante" FlowDirection="RightToLeft" Width="429"/>

而且我需要知道他们是否更改了使用此方法发送 Json 包:

if (Perfil.placechange == true)
        {
            int[] places = new int[7];
            for (int i = 0; i < 6; i++)
            {
                if (Perfil.placeTypes[i] == true)
                {
                    places[i] = i + 1;
                }
            }
            preferencias.placeTypes = places;
            launch = true;
        }
        paquete.preferencias = preferencias;
        if (launch == true)
        {
            Uri url = new Uri("link");
            string respuesta = await metodosJson.jsonPOST(url, paquete);
            var paqueteprueba = JsonConvert.SerializeObject(paquete);
            var respuestajson = JsonConvert.DeserializeObject<objetoslistas.setPreferencesoutput>(respuesta.ToString());
            if (respuestajson.error == "")
            {
                Settings["timestamp"] = respuestajson.timestamp;
            }
        }

当我启动模拟器时,一切似乎都正常,复选框被标记或未标记,具体取决于布尔值是真还是假。但是当我改变其中任何一个的状态时,placechange 永远不会变为真。事实是,如果我查看完整的对象,布尔数组已经改变。任何的想法?

4

2 回答 2

2

将数组更改为可观察的集合,该集合监视和报告任何内部集合更改,而不仅仅是对集合的引用已更改,并根据需要处理该信息。

更新

请参阅下面的 Muflar 回答,了解如何在添加内容后使用可观察集合来获取通知。

于 2013-10-28T13:06:21.343 回答
1

好的,最后我添加了

_placeTypes.CollectionChanged += _placeTypes_CollectionChanged;

到构造函数,然后是新方法:

void _placeTypes_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if(e.OldItems!=null)
        {
            placechange = true;
        }
    }

对象

于 2013-10-30T11:59:06.157 回答