Esempio n. 1
0
        private void LoadSettings()
        {
            var properties = Settings.GetType().GetProperties().Where(p => p.IsDefined(typeof(SettingsPropertyAttribute), false));

            foreach (var property in properties)
            {
                SettingsPropertyAttribute attribute = (SettingsPropertyAttribute)property.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(SettingsPropertyAttribute));
                if (attribute == null)
                {
                    continue;
                }

                if (!attribute.Save)
                {
                    continue;
                }

                string key = $"{SETTING_KEY}{property.Name}";
                if (!RegistryHelper.Instance.SubKey(Name).Exists(key))
                {
                    continue;
                }

                var value = RegistryHelper.Instance.SubKey(Name).GetObject(key, property.GetValue(Settings));
                property.SetValue(Settings, value);
            }
        }
Esempio n. 2
0
        private void SaveSettings()
        {
            var properties = Settings.GetType().GetProperties().Where(p => p.IsDefined(typeof(SettingsPropertyAttribute), false));

            foreach (var property in properties)
            {
                SettingsPropertyAttribute attribute = (SettingsPropertyAttribute)property.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(SettingsPropertyAttribute));
                if (attribute == null)
                {
                    continue;
                }

                if (!attribute.Save)
                {
                    continue;
                }

                RegistryHelper.Instance.SubKey(Name).Set($"{SETTING_KEY}{property.Name}", property.GetValue(Settings));
            }
        }
Esempio n. 3
0
        internal static UIElement GetUIElement(PropertyInfo propertyInfo, SettingsService settingsService,
                                               SettingsKey settingsKey, object defaultValue, SettingsPropertyAttribute attribute)
        {
            Brush fgBrush   = Application.Current.Resources[EnvironmentColors.BrandedUITextBrushKey] as Brush;
            var   labelName = attribute.Name ?? propertyInfo.Name;

            if (propertyInfo.PropertyType == typeof(bool))
            {
                var isChecked = settingsService.Get(settingsKey, (bool?)defaultValue ?? false);
                var res       = new CheckBox {
                    Content = labelName, IsChecked = isChecked
                };
                res.Checked   += (sender, args) => settingsService.Set(settingsKey, res.IsChecked ?? false);
                res.Unchecked += (sender, args) => settingsService.Set(settingsKey, res.IsChecked ?? false);
                res.ToolTip    = attribute.Description;
                return(res);
            }
            if (propertyInfo.PropertyType == typeof(string) || propertyInfo.PropertyType == typeof(int))
            {
                var editorAttribute = propertyInfo.GetCustomAttribute <EditorAttribute>();
                var grid            = new Grid();
                grid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = GridLength.Auto
                });
                grid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });
                if (editorAttribute != null)
                {
                    grid.ColumnDefinitions.Add(new ColumnDefinition {
                        Width = GridLength.Auto
                    });
                }

                var textBox = CreateTextBox(propertyInfo, settingsService, settingsKey, defaultValue);
                Grid.SetColumn(textBox, 1);
                grid.ToolTip = attribute.Description;

                grid.Children.Add(new Label {
                    Foreground = fgBrush, Content = labelName + ":", ToolTip = labelName
                });
                grid.Children.Add(textBox);
                if (editorAttribute != null)
                {
                    var uiTypeEditor = Activator.CreateInstance(Type.GetType(editorAttribute.EditorTypeName)) as UITypeEditor;
                    var panel        = new StackPanel();
                    Grid.SetColumn(panel, 2);
                    grid.Children.Add(panel);
                    if (editorAttribute.EditorTypeName.Contains(typeof(FileNameEditor).FullName))
                    {
                        var btnSelect = new Button {
                            Content = "...", Width = 22, Height = 21, Margin = new Thickness(-2, 2, 0, 0), MinHeight = 1, MinWidth = 1
                        };
                        btnSelect.Click += (sender, args) =>
                        {
                            var res = uiTypeEditor.EditValue(textBox.Text);
                            if (res != null)
                            {
                                textBox.Text = res;
                            }
                        };
                        panel.Children.Add(btnSelect);
                    }
                }
                return(grid);
            }
            if (propertyInfo.PropertyType.IsEnum)
            {
                var value = settingsService.Get(settingsKey, propertyInfo.PropertyType, defaultValue);
                var grid  = new UniformGrid {
                    Columns = 2
                };
                var combo = new ComboBox()
                {
                    Width         = 180,
                    Style         = Application.Current.TryFindResource("COABComboBoxStyle") as Style,
                    Margin        = new Thickness(0, 2, 2, 2),
                    SelectedValue = Enum.Parse(propertyInfo.PropertyType, value.ToString()),                    //value,
                    ItemsSource   = Enum.GetValues(propertyInfo.PropertyType)
                };
                combo.SelectionChanged += (sender, args) => settingsService.Set(propertyInfo.PropertyType, settingsKey, combo.SelectedValue);
                grid.ToolTip            = attribute.Description;
                grid.Children.Add(new Label {
                    Foreground = fgBrush, Content = labelName + ":"
                });
                grid.Children.Add(combo);
                return(grid);
            }

            if (propertyInfo.PropertyType.IsArray)
            {
                var grp = new GroupBox {
                    Header = labelName, Margin = new Thickness(0, 5, 0, 5)
                };
                var value      = settingsService.Get(settingsKey, new string[0]);
                var itemSource = new ObservableCollection <Bindable <string> >(value.Select(s => new Bindable <string>(s)));
                var edit       = new StringListEdit {
                    Height = 150, DataContext = itemSource, BrowseMode = BrowseMode.Directories
                };
                var dock = new DockPanel();
                dock.Children.Add(edit);
                Label labelDes = new Label {
                    Content = attribute.Description
                };
                DockPanel.SetDock(labelDes, Dock.Bottom);
                dock.Children.Add(labelDes);
                grp.Content = dock;
                PropertyChangedEventHandler saveAction = (sender, args) => settingsService.Set(settingsKey, itemSource.Select(b => b.Value).ToArray());
                itemSource.Apply(bindable => bindable.PropertyChanged += saveAction);
                itemSource.CollectionChanged += (sender, args) =>
                {
                    settingsService.Set(settingsKey, itemSource.Select(bindable => bindable.Value).ToArray());
                    args.NewItems?.OfType <Bindable <string> >().Apply(bindable => bindable.PropertyChanged += saveAction);
                    args.OldItems?.OfType <Bindable <string> >().Apply(bindable => bindable.PropertyChanged -= saveAction);
                };
                edit.LostFocus += (sender, args) => saveAction(sender, new PropertyChangedEventArgs(""));
                return(grp);
            }

            throw new NotSupportedException(propertyInfo.PropertyType + " is not supported");
        }