【问题标题】:Windows phone 8 array binding don't seems to changeWindows phone 8 数组绑定似乎没有改变
【发布时间】:2013-11-07 06:18:36
【问题描述】:

编辑:按照建议对 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 永远不会变为真。事实是,如果我查看完整的对象,布尔数组已经改变。有什么想法吗?

【问题讨论】:

  • 数组不提供通知功能。请改用 ObservableCollection。

标签: c# arrays mvvm binding windows-phone-8


【解决方案1】:

将数组更改为observable collection,它监视和报告任何内部集合更改,而不仅仅是对集合的引用发生了更改,并根据需要处理该信息。

更新

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

【讨论】:

    【解决方案2】:

    好的,最后我添加了

    _placeTypes.CollectionChanged += _placeTypes_CollectionChanged;
    

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

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

    对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-15
      • 2013-12-30
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      相关资源
      最近更新 更多