ProperyChanged事件总是null - Windows Phone

本文关键字:Windows Phone null 事件 ProperyChanged | 更新日期: 2023-09-27 18:06:38

我看过一些与这个问题相关的答案,但都不太像我的这个场景。我有一个设置类,当其中一个属性更新时,我希望通知另一个类。事件正在被触发,但其处理程序始终为空。

谁能告诉我是怎么回事?

class AppSettings : INotifyPropertyChanged
{
    // Our settings
    ApplicationDataContainer settings ;
    // The key names of our settings
    const string CheckBoxSettingKeyName = "CheckBoxSetting";
    const string ComboBoxSettingKeyName = "ComboBoxSetting";
    // The default value of our settings
    const bool CheckBoxSettingDefault = true;
    private int ComboBoxSettingDefault = 0;
    public event PropertyChangedEventHandler PropertyChanged;
    public AppSettings()
    {
        settings = ApplicationData.Current.LocalSettings;
    }
    public bool AddOrUpdateValue(string Key, Object value)
    {
        bool valueChanged = false;
        // If the key exists
        if (settings.Values.ContainsKey(Key))
        {
            // If the value has changed
            if (settings.Values[Key] != value)
            {
                // Store the new value
                settings.Values[Key] = value;
                valueChanged = true;
            }
        }
        // Otherwise create the key.
        else
        {
            settings.Values.Add(Key, value);
            valueChanged = true;
        }
        return valueChanged;
    }
    /// <summary>
    /// Get the current value of the setting, or if it is not found, set the 
    /// setting to the default setting.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="Key"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public T GetValueOrDefault<T>(string Key, T defaultValue)
    {
        T value;
        // If the key exists, retrieve the value.
        if (settings.Values.ContainsKey(Key))
        {
            value = (T)settings.Values[Key];
        }
        // Otherwise, use the default value.
        else
        {
            value = defaultValue;
        }
        return value;
    }
    /// Property to get and set a ComboBox Setting Key.
    public int ComboBoxSettings
    {
        get
        {
            return GetValueOrDefault<int>(ComboBoxSettingKeyName, ComboBoxSettingDefault);
        }
        set
        {
            AddOrUpdateValue(ComboBoxSettingKeyName, value);
            NotifyPropertyChanged();
        }
    }
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

}

ProperyChanged事件总是null - Windows Phone

我删除了一些代码并创建了一个新的控制台应用程序。然后我把Program class改成:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    class Program
    {
        static void Main(string[] args)
        {
            // create instance
            var settings = new AppSettings();
            // subscribe for the event as soon as you can
            settings.PropertyChanged += (s, e) => Console.WriteLine("Property {0} has changed", e.PropertyName);
            Console.WriteLine("Press any key to start test");
            Console.ReadKey();
            // change the value
            settings.ComboBoxSettings = 10;
            Console.ReadKey();
        }
    }
    class AppSettings : INotifyPropertyChanged
    {
        private int _comboBoxSettings;
        public event PropertyChangedEventHandler PropertyChanged;
        public int ComboBoxSettings
        {
            get
            {
                return _comboBoxSettings;
            }
            set
            {
                _comboBoxSettings = value;
                NotifyPropertyChanged();
            }
        }
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    #endregion
}

}

如果你把它传递到一个新的控制台应用程序,它会工作。