当可观察集合中的属性包含另一个可观察集合时,UWP C#UI不会更新

时间:2020-09-27 21:42:02

标签: c# uwp binding observable observablecollection

更新:添加了最小的可复制示例国家名称正在更新,Hello1,Hello2 ....,但是,城市温度未更新https://github.com/qaftab/Observable

这是我的问题:添加对象时,UI将会更新。但是,当Country.CitiesInCountry内部的属性值更改时,UI不会更新。当我早些时候问这个问题时,建议我添加我添加的BindableBase类。

因此,当城市或国家(地区)对象中的任何属性更改时,我正在寻求帮助来修改下面的类以更新UI(与国家/地区绑定)。

public class City : BindableBase
{
    public string Name { set; get; }
    /*
           public int Temperature { set; get; }

           public int TEMPERATURE
           {
               get { return Temperature; }
               set
               {
                   Temperature = value;
                   OnPropertyChanged("TEMPERATURE");
               }
           }*/
    public int Temperature;
    public int TEMPERATURE
    {
        get => Temperature;
        set => SetProperty(ref Temperature, value);
        
    }

}


public class Country:BindableBase
{
    //public string CountryName { get; set; }
    private string countryName;
    public string CountryName
    {
        get => countryName;
        set => SetProperty(ref countryName, value);
    }



    // public ObservableCollection<City> CitiesInCountry = new ObservableCollection<City>();
    private ObservableCollection<City> CitiesInCountry = new ObservableCollection<City>();
    public ObservableCollection<City> CITIES
    {
        get => CitiesInCountry;
        set => SetProperty(ref CitiesInCountry, value);
    }


}


 [Windows.Foundation.Metadata.WebHostHidden]
    public abstract class BindableBase : INotifyPropertyChanged
    {
        /// <summary>
        /// Multicast event for property change notifications.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Checks if a property already matches a desired value.  Sets the property and
        /// notifies listeners only when necessary.
        /// </summary>
        /// <typeparam name="T">Type of the property.</typeparam>
        /// <param name="storage">Reference to a property with both getter and setter.</param>
        /// <param name="value">Desired value for the property.</param>
        /// <param name="propertyName">Name of the property used to notify listeners.  This
        /// value is optional and can be provided automatically when invoked from compilers that
        /// support CallerMemberName.</param>
        /// <returns>True if the value was changed, false if the existing value matched the
        /// desired value.</returns>
        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        /// <summary>
        /// Notifies listeners that a property value has changed.
        /// </summary>
        /// <param name="propertyName">Name of the property used to notify listeners.  This
        /// value is optional and can be provided automatically when invoked from compilers
        /// that support <see cref="CallerMemberNameAttribute"/>.</param>
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

0 个答案:

没有答案
相关问题