コード例 #1
0
 protected virtual void OnSettingChanging(SettingChangingEventArgs e)
 {
     SettingChanging?.Invoke(this, e);
 }
コード例 #2
0
        public SetSettingResult SetSetting <T>(string key, T value, StorageLocation storageLocation = StorageLocation.Local)
        {
            Type settingType     = typeof(T);
            var  settingTypeInfo = settingType.GetTypeInfo();
            bool xmlConvertible  = xmlConvertibleTypeInfo.IsAssignableFrom(settingTypeInfo);

            /* Check to see if the value is already set.
             * This avoids raising events unnecessarily. */
            bool alreadySet;

            lockSlim.EnterWriteLock();
            try
            {
                alreadySet = false;
                cache.Remove(key);

                if (localStore.Status == SettingsStoreStatus.Ready && localStore.TryGetValue(key, settingType, out object existingValue) ||
                    (roamingStore != null && roamingStore.Status == SettingsStoreStatus.Ready && roamingStore.TryGetValue(key, settingType, out existingValue)) ||
                    (transientStore != null && transientStore.Status == SettingsStoreStatus.Ready && transientStore.TryGetValue(key, settingType, out existingValue)))
                {
                    if (existingValue == null && value == null || existingValue != null && existingValue.Equals(value))
                    {
                        alreadySet = true;
                    }
                    else if (xmlConvertible)
                    {
                        if (existingValue != null)
                        {
                            Type concreteType = settingType.IsInterface() ? value?.GetType() : settingType;

                            if (concreteType != null)
                            {
                                if (TryConvertFromXml(concreteType, existingValue, out object existingObject))
                                {
                                    if (Equals(existingObject, value))
                                    {
                                        alreadySet = true;
                                    }
                                }
                            }
                        }
                    }
                    else if (existingValue != null && settingType.IsEnum() && existingValue is int)
                    {
                        int intValue = (int)(object)value;                        //(int)Convert.ChangeType(value, typeof(int));
                        if (AreEqual(intValue, existingValue))
                        {
                            alreadySet = true;
                        }
                    }
                    else if (existingValue != null && existingValue is byte[] && settingType != typeof(byte[]))
                    {
                        var serializer     = Dependency.Resolve <IBinarySerializer, BinarySerializer>();
                        var existingObject = serializer.Deserialize <object>((byte[])existingValue);
                        if (AreEqual(value, existingObject))
                        {
                            alreadySet = true;
                        }
                    }
                }
            }
            finally
            {
                lockSlim.ExitWriteLock();
            }

            if (alreadySet)
            {
                return(SetSettingResult.Successful);
            }

            /* Allows this action to be cancelled. */
            var args = new SettingChangingEventArgs(key, value);

            OnSettingChanging(args);

            if (args.Cancel)
            {
                Debug.WriteLine($"Setting change cancelled. Key: {key} New Value: {value}");
                return(SetSettingResult.Cancelled);
            }

            lockSlim.EnterWriteLock();
            try
            {
                string cacheKey = key;
                cache[cacheKey] = value;

                if (storageLocation == StorageLocation.Transient)
                {
                    if (transientStore.Status == SettingsStoreStatus.Ready)
                    {
                        SetTransientStateValue(transientStore, key, value);
                    }
                    else
                    {
                        /* SettingsService is being used before app is completely initialized. */
                        UIContext.Instance.Post(delegate
                        {
                            if (transientStore.Status == SettingsStoreStatus.Ready)
                            {
                                SetTransientStateValue(transientStore, key, value);
                            }
                        });
                    }
                }
                else
                {
                    ISettingsStore store;
                    if (storageLocation == StorageLocation.Roaming)
                    {
                        store = roamingStore ?? localStore;
                    }
                    else
                    {
                        store = localStore;
                    }

                    if (store.Status == SettingsStoreStatus.Ready)
                    {
                        SaveValueToStore(store, key, value, xmlConvertible, settingType);
                    }
                    else
                    {
                        /* We push the save value request onto the UI thread queue in case
                         * some further initialization needs to occur. Otherwise it is up to
                         * the ISettingsStore implementation to queue the update. */
                        UIContext.Instance.Post(delegate
                        {
                            if (store.Status == SettingsStoreStatus.Ready)
                            {
                                SaveValueToStore(store, key, value, xmlConvertible, settingType);
                            }
                        });
                    }
                }
            }
            finally
            {
                lockSlim.ExitWriteLock();
            }

            OnSettingChanged(new SettingChangeEventArgs(key, value));

            return(SetSettingResult.Successful);
        }