示例#1
0
文件: Store.cs 项目: atum201/ioio
        private static void SetValue(object value, [CallerMemberName] string key = "")
        {
            //Updates or inserts the value to the Local resource.
            if (_local != null)
            {
                if (_local.Contains(key))
                {
                    _local[key] = value;

                    //If the value is being set to null, remove it.
                    if (value == null && (!Default.Contains(key) || Default[key] == null))
                    {
                        _local.Remove(key);
                    }
                }
                else
                {
                    _local.Add(key, value);
                }
            }


            //Updates/Adds the current value of the resource.
            if (Application.Current.Resources.Contains(key))
            {
                Application.Current.Resources[key] = value;
            }
            else
            {
                Application.Current.Resources.Add(key, value);
            }

            All.OnPropertyChanged(key);
        }
示例#2
0
            public void UpdateResources(ResourceDictionary resources, Color?backgroundColor, Color?foregroundColor)
            {
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                if ((Usage & ColorUsage.Background) == ColorUsage.Background)
                {
                    var key = new ThemeResourceKey(m_parent.CategoryGuid, Name, ThemeResourceKeyType.BackgroundBrush);
                    if (backgroundColor.HasValue)
                    {
                        var brush = new SolidColorBrush(backgroundColor.Value);
                        brush.Freeze();
                        resources[key] = brush;
                    }
                    else
                    {
                        resources.Remove(key);
                    }
                }

                if ((Usage & ColorUsage.Foreground) == ColorUsage.Foreground)
                {
                    var key = new ThemeResourceKey(m_parent.CategoryGuid, Name, ThemeResourceKeyType.ForegroundBrush);
                    if (foregroundColor.HasValue)
                    {
                        var brush = new SolidColorBrush(foregroundColor.Value);
                        brush.Freeze();
                        resources[key] = brush;
                    }
                    else
                    {
                        resources.Remove(key);
                    }
                }
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
            }
        protected void ResourceRemoveEntry(ResourceDictionary dictionary)
        {
            if (dictionary.Count == 0)
            {
                return;
            }

            if (IsRemoveAll)
            {
                dictionary.Clear();
            }
            else
            {
                RemoveIndex %= dictionary.Count;
                //Get key at RemoveIndex.
                object key   = null;
                int    count = 0;
                foreach (object item in dictionary.Keys)
                {
                    if (count == RemoveIndex)
                    {
                        key = item;
                        break;
                    }
                    count++;
                }

                if (key != null)
                {
                    dictionary.Remove(key);
                }
            }
        }
示例#4
0
 public void AddTest1()
 {
     tlog.Debug(tag, $"AddTest1 START");
     try
     {
         ResourceDictionary t2 = new ResourceDictionary();
         Assert.IsNotNull(t2, "null ResourceDictionary");
         t2.Add("AA", "AA");
         var ret = t2.ContainsKey("AA");
         Assert.AreEqual(true, ret, "Should be equal");
         var ret2 = t2["AA"];
         Assert.AreEqual("AA", ret2, "Should be equal");
         Assert.AreEqual(1, t2.Keys.Count, "Should be equal");
         Assert.AreEqual(1, t2.Values.Count, "Should be equal");
         Assert.IsNotNull(t2.GetEnumerator(), "null Enumerator");
         object ss;
         t2.TryGetValue("AA", out ss);
         Assert.AreEqual("AA", ss as string, "Should be equal");
         var ret3 = t2.Remove("AA");
         Assert.True(ret3, "Should be true");
     }
     catch (Exception e)
     {
         Assert.Fail("Caught Exception" + e.ToString());
     }
     tlog.Debug(tag, $"AddTest1 END");
 }
        private void DarkMode_Unchecked(object sender, RoutedEventArgs e)
        {
            //Jasny motyw

            SolidColorBrush backgroundColor = new SolidColorBrush(Color.FromRgb(224, 224, 224));
            //SolidColorBrush whiteSmoke = new SolidColorBrush(Color.FromRgb(225, 229, 229));
            SolidColorBrush    textAreaColor        = new SolidColorBrush(Color.FromRgb(245, 245, 245));
            ResourceDictionary myResourceDictionary = Application.Current.Resources;

            myResourceDictionary.Remove("MaterialDesignBackground");
            myResourceDictionary.Add("MaterialDesignBackground", backgroundColor); //  #FFE5E5E5
            myResourceDictionary.Remove("GlobalForeColor");
            myResourceDictionary.Add("GlobalForeColor", Brushes.Black);            //Black
            myResourceDictionary.Remove("TextAreasBacground");
            myResourceDictionary.Add("TextAreasBacground", textAreaColor);         //FFA6A0A0
        }
示例#6
0
        protected void RegisterInjection(Type viewModelType, Type viewType)
        {
            // The only way to build DataTemplate from code is to parse XAML
            // The FrameworkElementFactory way is now deprecated and does not support some XAML elements
            var parserContext = new ParserContext();

            parserContext.XmlnsDictionary.Add(string.Empty, "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            parserContext.XmlnsDictionary.Add("v", "clr-namespace:" + viewType.Namespace + ";assembly=" + viewType.Assembly.GetName().Name);
            string xamlDataTemplate =
                "<DataTemplate>" +
                "<v:" + viewType.Name + "/>" +
                "</DataTemplate>";
            var dataTemplate = (DataTemplate)XamlReader.Parse(xamlDataTemplate, parserContext);

            dataTemplate.DataType = viewModelType;
            var dataTemplateKey = new DataTemplateKey(dataTemplate.DataType);

            foreach (var key in ResourceDictionary.Keys)
            {
                if (Equals(key, dataTemplateKey))
                {
                    var oldDataTemplate = (DataTemplate)ResourceDictionary[key];
                    var oldType         = oldDataTemplate.Template.GetType();
                    Logger.Log(Logger.Level.Info, $"The view model {viewModelType.Name} previously associated with the view {oldType.Name} has been redefined in the view injection by the view {viewType.Name}");
                    ResourceDictionary.Remove(key);
                }
            }
            ResourceDictionary.Add(dataTemplateKey, dataTemplate);
        }
示例#7
0
        private void SetThemeSource(Uri source, bool useThemeAccentColor)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var oldThemeDict = GetThemeDictionary();
            var dictionaries = Application.Current.Resources.MergedDictionaries;
            var themeDict    = new ResourceDictionary {
                Source = source
            };

            var accentColor = themeDict[KeyAccentColor] as Color?;

            if (accentColor.HasValue)
            {
                themeDict.Remove(KeyAccentColor);

                if (useThemeAccentColor)
                {
                    ApplyAccentColor(accentColor.Value);
                }
            }

            dictionaries.Add(themeDict);

            if (oldThemeDict != null)
            {
                dictionaries.Remove(oldThemeDict);
            }

            OnPropertyChanged("ThemeSource");
        }
        public void AddTheme(ResourceDictionary resourceTheme, bool useThemeAccentColor)
        {
            // if theme defines an accent color, use it
            var accentColor = resourceTheme[KeyAccentColor] as Color?;

            if (accentColor.HasValue)
            {
                // remove from the theme dictionary and apply globally if useThemeAccentColor is true
                resourceTheme.Remove(KeyAccentColor);

                if (useThemeAccentColor)
                {
                    ApplyAccentColor(accentColor.Value);
                }
            }

            var dictionaries = Application.Current.Resources.MergedDictionaries;

            var oldResourceTheme = GetThemeDictionary();

            // add new before removing old theme to avoid dynamicresource not found warnings
            dictionaries.Add(resourceTheme);

            // remove old theme
            if (oldResourceTheme != null)
            {
                dictionaries.Remove(oldResourceTheme);
            }
        }
        static void SetThemeSource(Uri source)
        {
            var oldThemeDict = GetThemeDictionary();
            var dictionaries = Application.Current.Resources.MergedDictionaries;
            var themeDict    = new ResourceDictionary {
                Source = source
            };

            // if theme defines an accent color, use it
            var accentColor = themeDict[KeyAccentColor] as Color?;

            if (accentColor.HasValue)
            {
                // remove from the theme dictionary and apply globally if useThemeAccentColor is true
                themeDict.Remove(KeyAccentColor);
            }

            // add new before removing old theme to avoid dynamicresource not found warnings
            dictionaries.Add(themeDict);

            // remove old theme
            if (oldThemeDict != null)
            {
                dictionaries.Remove(oldThemeDict);
            }
        }
示例#10
0
 private static void DetachStyleTypes(ResourceDictionary resourceDictionary, IEnumerable <Type> types)
 {
     foreach (var type in types)
     {
         resourceDictionary.Remove(type);
     }
 }
示例#11
0
        private void DarkMode_Checked(object sender, RoutedEventArgs e)
        {
            //Ustawienia Ciemego motywu

            SolidColorBrush    backgroundColor      = new SolidColorBrush(Color.FromArgb(250, 18, 18, 18));
            SolidColorBrush    textAreaColor        = new SolidColorBrush(Color.FromArgb(120, 18, 18, 18));
            SolidColorBrush    foreColor            = new SolidColorBrush(Color.FromRgb(236, 240, 241));
            ResourceDictionary myResourceDictionary = Application.Current.Resources;

            ///MessageBox.Show(myResourceDictionary.Values.ToString());
            myResourceDictionary.Remove("MaterialDesignBackground");
            myResourceDictionary.Add("MaterialDesignBackground", backgroundColor);   //"#121212"       rgb(22, 160, 133)
            myResourceDictionary.Remove("GlobalForeColor");
            myResourceDictionary.Add("GlobalForeColor", foreColor);
            myResourceDictionary.Remove("TextAreasBacground");
            myResourceDictionary.Add("TextAreasBacground", textAreaColor); //TextAreasBacground
        }
示例#12
0
        public static void RemoveKey(this ResourceDictionary values, string key)
        {
            var value = values[key];

            if (value != null)
            {
                values.Remove(value);
            }
        }
示例#13
0
        private static void SetResourceStyle(ResourceDictionary resourceDictionary, ThemeManagerStyle style)
        {
            if (resourceDictionary.Contains(style.TargetType))
            {
                resourceDictionary.Remove(style.TargetType);
            }

            resourceDictionary.Add(style.TargetType, style.NativeStyle);
        }
示例#14
0
        private static void ApplyResourceDictionary(ResourceDictionary newRd, ResourceDictionary oldRd)
        {
            foreach (DictionaryEntry r in newRd)
            {
                if (oldRd.Contains(r.Key))
                    oldRd.Remove(r.Key);

                oldRd.Add(r.Key, r.Value);
            }
        }
        private static bool TryRemoveFromResourceDictionary(ResourceDictionary resourceDictionary, string keyName)
        {
            if (!resourceDictionary.Contains(keyName))
            {
                return(false);
            }

            resourceDictionary.Remove(keyName);

            return(true);
        }
示例#16
0
 public static void PutMany(this ResourceDictionary dict, ResourceDictionary source)
 {
     foreach (var entry in source.AsEnumerable())
     {
         if (dict.ContainsKey(entry.Key))
         {
             dict.Remove(entry.Key);
         }
         dict.Add(entry.Key, entry.Value);
     }
 }
示例#17
0
 private static void ApplyResourceDictionary(ResourceDictionary newRd, ResourceDictionary oldRd)
 {
     oldRd.BeginInit();
     foreach (DictionaryEntry entry in newRd)
     {
         if (oldRd.Contains(entry.Key))
         {
             oldRd.Remove(entry.Key);
         }
         oldRd.Add(entry.Key, entry.Value);
     }
     oldRd.EndInit();
 }
示例#18
0
        public override void CleanResources()
        {
            ResourceDictionary.ToList().ForEach(s =>
            {
                s.Value.Dispose();
                ResourceDictionary.Remove(s.Key);
            });

            _landDictionary.ToList().ForEach(s =>
            {
                s.Value.Dispose();
                _landDictionary.Remove(s.Key);
            });
        }
示例#19
0
        public virtual void CleaUnusedResources()
        {
            long ticks = Engine.Ticks - Constants.CLEAR_TEXTURES_DELAY;

            ResourceDictionary
            .Where(s => s.Value.Ticks < ticks)
            .Take(Constants.MAX_GUMP_OBJECT_REMOVED_BY_GARBAGE_COLLECTOR)
            .ToList()
            .ForEach(s =>
            {
                s.Value.Dispose();
                ResourceDictionary.Remove(s.Key);
            });
        }
示例#20
0
 public static void RemoveShades(ResourceDictionary colors)
 {
     colors.Remove(AccentDark3Key);
     colors.Remove(AccentDark2Key);
     colors.Remove(AccentDark1Key);
     colors.Remove(AccentLight1Key);
     colors.Remove(AccentLight2Key);
     colors.Remove(AccentLight3Key);
 }
示例#21
0
        public static void SetLanguage(string language)
        {
            var dictionaries = Application.Current.Resources.MergedDictionaries;

            if (CurrentLanguage != SourceLanguageId)
            {
                var currentLang = dictionaries.FirstOrDefault(a => a["LanguageName"] != null && a.Source == null);
                if (currentLang != null)
                {
                    dictionaries.Remove(currentLang);
                }
            }

            var langFile = Path.Combine(PlaynitePaths.LocalizationsPath, language + ".xaml");

            if (File.Exists(langFile) && language != SourceLanguageId)
            {
                ResourceDictionary res = null;
                try
                {
                    res        = Xaml.FromFile <ResourceDictionary>(langFile);
                    res.Source = new Uri(langFile, UriKind.Absolute);
                    // Unstranslated strings are imported as empty entries by Crowdin.
                    // We need to remove them to make sure that origina English text will be displayed instead.
                    foreach (var key in res.Keys)
                    {
                        if (res[key] is string locString && locString.IsNullOrEmpty())
                        {
                            res.Remove(key);
                        }
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, $"Failed to parse localization file {langFile}");
                    return;
                }

                dictionaries.Add(res);

                ApplicationLanguageCultureInfo = new CultureInfo(language.Replace("_", "-"), false);
            }
            else
            {
                ApplicationLanguageCultureInfo = new CultureInfo("en-US", false); // english is the default language
            }

            CurrentLanguage = language;
        }
示例#22
0
        /// <summary>
        /// Set in application ressources the common ressources.
        /// </summary>
        /// <param name="pluginFolder"></param>
        public static void Load(string pluginFolder)
        {
            List <string> ListCommonFiles = new List <string>
            {
                Path.Combine(pluginFolder, "Resources\\Common.xaml"),
                Path.Combine(pluginFolder, "Resources\\LiveChartsCommon\\Common.xaml")
            };

            foreach (string CommonFile in ListCommonFiles)
            {
                if (File.Exists(CommonFile))
                {
#if DEBUG
                    logger.Debug($"PluginCommon - Load {CommonFile}");
#endif

                    ResourceDictionary res = null;
                    try
                    {
                        res        = Xaml.FromFile <ResourceDictionary>(CommonFile);
                        res.Source = new Uri(CommonFile, UriKind.Absolute);

                        foreach (var key in res.Keys)
                        {
                            if (res[key] is string locString && locString.IsNullOrEmpty())
                            {
                                res.Remove(key);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogError(ex, "PluginCommon", $"Failed to parse file {CommonFile}");
                        return;
                    }

#if DEBUG
                    logger.Debug($"PluginCommon - res: {JsonConvert.SerializeObject(res)}");
#endif
                    Application.Current.Resources.MergedDictionaries.Add(res);
                }
                else
                {
                    logger.Warn($"PluginCommon - File {CommonFile} not found.");
                    return;
                }
            }
        }
示例#23
0
        public void When_Created_From_Source_In_Codebehind()
        {
            var rd = new ResourceDictionary {
                Source = new Uri("ms-appx:///Uno.UI.Tests.ViewLibrary/Themes/More/Test_Dictionary_Abs.xaml")
            };

            AssertEx.AssertContainsColorBrushResource(rd, "BituminousColorBrush", Colors.SlateGray);

            var rd2 = new ResourceDictionary {
                Source = new Uri("ms-appx:///Uno.UI.Tests.ViewLibrary/Themes/More/Test_Dictionary_Abs.xaml")
            };

            rd2.Remove("BituminousColorBrush");
            Assert.IsFalse(rd2.ContainsKey("BituminousColorBrush"));
            Assert.IsTrue(rd.ContainsKey("BituminousColorBrush"));
        }
示例#24
0
        public static void SetResourceValue(this ResourceDictionary resourceDictionary, object key, object value)
        {
#if SILVERLIGHT
            if (resourceDictionary.Contains(key))
            {
                resourceDictionary.Remove(key);
                resourceDictionary.Add(key, value);
            }
            else
            {
                resourceDictionary.Add(key, value);
            }
#else
            resourceDictionary[key] = value;
#endif
        }
示例#25
0
        private void ApplyResourceDictionaryEntries(ResourceDictionary oldRd, ResourceDictionary newRd)
        {
            foreach (var newRdMergedDictionary in newRd.MergedDictionaries)
            {
                ApplyResourceDictionaryEntries(oldRd, newRdMergedDictionary);
            }

            foreach (DictionaryEntry dictionaryEntry in newRd)
            {
                if (oldRd.Contains(dictionaryEntry.Key))
                {
                    oldRd.Remove(dictionaryEntry.Key);
                }

                oldRd.Add(dictionaryEntry.Key, dictionaryEntry.Value);
            }
        }
示例#26
0
        public void PostProcessDictionary(Theme theme, ResourceDictionary resourceDictionary)
        {
            foreach (var dict in resourceDictionary.MergedDictionaries)
            {
                PostProcessDictionary(theme, dict);
            }

            foreach (var resource in resourceDictionary)
            {
                if (resource.Value is Style style && _processedTargetTypes.Any(t => t.IsAssignableFrom(style.TargetType)))
                {
                    var newKey = Strings.Normalise($"{theme.Name} {style.TargetType.FullName.Replace('.', ' ')} {resource.Key}", '_', false);
                    resourceDictionary.Remove(resource.Key);
                    resourceDictionary.Add(newKey, resource.Value);
                }
            }
        }
示例#27
0
        private void SetThemeSource(Uri source, bool useThemeAccentColor)
        {
            try
            {
                if (source == null)
                {
                    throw new ArgumentNullException("source");
                }

                var oldThemeDict = GetThemeDictionary();
                var dictionaries = Application.Current.Resources.MergedDictionaries;

                var themeDict = new ResourceDictionary {
                    Source = source
                };

                // if theme defines an accent color, use it
                var accentColor = themeDict[KeyAccentColor] as Color?;
                if (accentColor.HasValue)
                {
                    // remove from the theme dictionary and apply globally if useThemeAccentColor is true
                    themeDict.Remove(KeyAccentColor);

                    if (useThemeAccentColor)
                    {
                        ApplyAccentColor(accentColor.Value);
                    }
                }

                // add new before removing old theme to avoid dynamicresource not found warnings
                dictionaries.Add(themeDict);

                // remove old theme
                if (oldThemeDict != null)
                {
                    dictionaries.Remove(oldThemeDict);
                }

                OnPropertyChanged("ThemeSource");
            }
            catch (Exception ex)
            {
                throw new System.Exception(ex.Message);
            }
        }
示例#28
0
        /// <summary>
        ///     Releases or completely removes a resource.
        /// </summary>
        /// <param name="style">The style.</param>
        /// <param name="release">if set to <see langword="true" /> the resource will be released instead of completely removed.</param>
        private bool ReleaseRemove(IStyle style, bool release)
        {
            if (style == null)
            {
                throw new ArgumentNullException(nameof(style));
            }

            // If we are disposed, then technically we have already released the style
            if (_brushes == null)
            {
                return(true);
            }
            lock (_lock)
            {
                if (_brushes == null)
                {
                    return(true);
                }

                ImageStyle imageStyle = style as ImageStyle;
                if (imageStyle != null)
                {
                    Release(imageStyle.Image);
                }

                Resource <Brush> brush;
                switch (_brushes.Remove(style, out brush, release ? true : (bool?)null))
                {
                case Removed.NotFound:
                    return(false);

                case Removed.Removed:
                    return(true);

                case Removed.RemovedLast:
                    Debug.Assert(brush != null, "brush != null");
                    brush.Dispose();
                    return(true);

                default:
                    Debug.Fail("Unexpected value");
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
        public static void SetPluginLanguage(string pluginFolder, string language, bool DefaultLoad = false)
        {
            // Load default for missing
            if (!DefaultLoad)
            {
                SetPluginLanguage(pluginFolder, "LocSource", true);
            }

            var dictionaries = Application.Current.Resources.MergedDictionaries;
            var langFile     = Path.Combine(pluginFolder, "Localization\\" + language + ".xaml");

            // Load localization
            if (File.Exists(langFile))
            {
                ResourceDictionary res = null;
                try
                {
                    using (var stream = new StreamReader(langFile))
                    {
                        res        = (ResourceDictionary)XamlReader.Load(stream.BaseStream);
                        res.Source = new Uri(langFile, UriKind.Absolute);
                    }

                    foreach (var key in res.Keys)
                    {
                        if (res[key] is string locString && String.IsNullOrEmpty(locString))
                        {
                            res.Remove(key);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex, $"Failed to parse localization file {langFile}.");
                    return;
                }

                dictionaries.Add(res);
            }
            else
            {
                logger.Warn($"File {langFile} not found.");
            }
        }
示例#30
0
        private void ApplyResourceDictionaryEntries(ResourceDictionary oldRd, ResourceDictionary newRd)
        {
            foreach (var newRdMergedDictionary in newRd.MergedDictionaries)
            {
                this.ApplyResourceDictionaryEntries(oldRd, newRdMergedDictionary);
            }

#pragma warning disable CS8605
            foreach (DictionaryEntry dictionaryEntry in newRd)
            {
                if (oldRd.Contains(dictionaryEntry.Key))
                {
                    oldRd.Remove(dictionaryEntry.Key);
                }

                oldRd.Add(dictionaryEntry.Key, dictionaryEntry.Value);
            }
#pragma warning restore CS8605
        }