Esempio n. 1
0
 public static void Merge(this ResourceDictionary resourceDictionary, ResourceDictionary mergee)
 {
     if (resourceDictionary != null && mergee != null && mergee.Count > 0)
     {
         foreach (var key in mergee.Keys)
         {
             if (!resourceDictionary.Contains(key))
             {
                 resourceDictionary.Add(key, mergee[key]);
             }
         }
     }
 }
Esempio n. 2
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();
 }
Esempio n. 3
0
        internal static String ReturnResourceString(string key, string dictionary)
        {
            var resourceDictionary = new ResourceDictionary();

            resourceDictionary.Source = new Uri(dictionary, UriKind.RelativeOrAbsolute);

            if (resourceDictionary.Contains(key))
            {
                var value = resourceDictionary[key];
                return(value.ToString());
            }
            return(null);
        }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var source = new ResourceDictionary {
                Source = new Uri(Path, UriKind.RelativeOrAbsolute)
            };
            var attribute = item?.GetType().GetCustomAttribute <DescriptionAttribute>();

            if (attribute != null && source.Contains(attribute.Description))
            {
                return(source[attribute.Description] as DataTemplate);
            }
            return(null);
        }
Esempio n. 5
0
        public static ResourceDictionary CreateResourceDictionary(ResourceDictionary dict, string[] propsToCopy)
        {
            var res = new ResourceDictionary();

            foreach (var key in propsToCopy)
            {
                if (dict.Contains(key))
                {
                    res[key] = dict[key];
                }
            }
            return(res);
        }
Esempio n. 6
0
        public void XamlLoadSharedTest()
        {
            string text = @"
            <ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:test='clr-namespace:Granular.Presentation.Tests.Markup;assembly=Granular.Presentation.Tests'>
                <test:LoaderTestElement x:Shared='false' x:Key='item1' Value1='1' Value2='2'/>
                <test:LoaderTestElement x:Shared='true' x:Key='item2' Value1='3' Value2='4'/>
            </ResourceDictionary>";

            XamlElement        rootElement = XamlParser.Parse(text);
            ResourceDictionary dictionary  = XamlLoader.Load(rootElement) as ResourceDictionary;

            Assert.IsNotNull(dictionary);
            Assert.IsTrue(dictionary.Contains("item1"));
            Assert.IsTrue(dictionary.Contains("item2"));

            LoaderTestElement item1a = dictionary.GetValue("item1") as LoaderTestElement;

            Assert.IsNotNull(item1a);
            Assert.AreEqual(1, ((LoaderTestElement)item1a).Value1);
            Assert.AreEqual(2, ((LoaderTestElement)item1a).Value2);

            LoaderTestElement item1b = dictionary.GetValue("item1") as LoaderTestElement;

            Assert.IsFalse(ReferenceEquals(item1a, item1b));
            Assert.IsNotNull(item1b);
            Assert.AreNotEqual(item1a, item1b);
            Assert.AreEqual(1, ((LoaderTestElement)item1b).Value1);
            Assert.AreEqual(2, ((LoaderTestElement)item1b).Value2);

            LoaderTestElement item2a = dictionary.GetValue("item2") as LoaderTestElement;

            Assert.IsNotNull(item2a);
            Assert.AreEqual(3, ((LoaderTestElement)item2a).Value1);
            Assert.AreEqual(4, ((LoaderTestElement)item2a).Value2);

            LoaderTestElement item2b = dictionary.GetValue("item2") as LoaderTestElement;

            Assert.IsTrue(ReferenceEquals(item2a, item2b));
        }
Esempio n. 7
0
        public static string FindResource(string key)
        {
            if (resource.Source == null)
            {
                try {
                    resource.Source = new Uri("pack://application:,,,/CNC.Controls.WPF;Component/LibStrings.xaml", UriKind.Absolute);
                }
                catch
                {
                }
            }

            return(resource.Source == null || !resource.Contains(key) ? string.Empty : (string)resource[key]);
        }
        private static Color GetForegroundColor(ResourceDictionary resourceDictionary)
        {
            if (resourceDictionary == null)
            {
                return(Colors.Transparent);
            }

            if (resourceDictionary.Contains(EditorFormatDefinition.ForegroundColorId))
            {
                var color = (Color)resourceDictionary[EditorFormatDefinition.ForegroundColorId];
                return(color);
            }

            if (resourceDictionary.Contains(EditorFormatDefinition.ForegroundBrushId))
            {
                if (resourceDictionary[EditorFormatDefinition.ForegroundBrushId] is SolidColorBrush brush)
                {
                    return(brush.Color);
                }
            }

            return(Colors.Transparent);
        }
Esempio n. 9
0
 public static void UpdateBrushes(ResourceDictionary themeDictionary, ResourceDictionary colors)
 {
     foreach (DictionaryEntry entry in themeDictionary)
     {
         if (entry.Value is SolidColorBrush brush)
         {
             object colorKey = ThemeResourceHelper.GetColorKey(brush);
             if (colorKey != null && colors.Contains(colorKey))
             {
                 brush.SetCurrentValue(SolidColorBrush.ColorProperty, (Color)colors[colorKey]);
             }
         }
     }
 }
        Brush GetForeGroundColor(string type, Brush fallbackBrush)
        {
            ResourceDictionary resourceDictionary = _editorFormatMap.GetProperties(type);

            if (resourceDictionary.Contains(EditorFormatDefinition.ForegroundBrushId))
            {
                var color = (Brush)resourceDictionary[EditorFormatDefinition.ForegroundBrushId];
                return(color);
            }
            else
            {
                return(fallbackBrush);
            }
        }
Esempio n. 11
0
        protected virtual void OnChildAdded(IPlotterElement child)
        {
            if (child != null)
            {
                addingElements.Push(child);
                currentChild = child;
                try
                {
                    UIElement visualProxy = CreateVisualProxy(child);
                    visualBindingCollection.Cache.Add(child, visualProxy);

                    if (performChildChecks && child.Plotter != null)
                    {
                        throw new InvalidOperationException(Strings.Exceptions.PlotterElementAddedToAnotherPlotter);
                    }

                    FrameworkElement styleableElement = child as FrameworkElement;
                    if (styleableElement != null)
                    {
                        Type key = styleableElement.GetType();
                        if (genericResources.Contains(key))
                        {
                            Style elementStyle = (Style)genericResources[key];
                            styleableElement.Style = elementStyle;
                        }
                    }

                    if (performChildChecks)
                    {
                        child.OnPlotterAttached(this);
                        if (child.Plotter != this)
                        {
                            throw new InvalidOperationException(Strings.Exceptions.InvalidParentPlotterValue);
                        }
                    }

                    DependencyObject dependencyObject = child as DependencyObject;
                    if (dependencyObject != null)
                    {
                        SetPlotter(dependencyObject, this);
                    }
                }
                finally
                {
                    addingElements.Pop();
                    currentChild = null;
                }
            }
        }
Esempio n. 12
0
        private static void CompareResourceDictionaries(ResourceDictionary first, ResourceDictionary second, params string[] ignoredKeyValues)
        {
            foreach (var key in first.Keys)
            {
                if (ignoredKeyValues.Contains(key) == false)
                {
                    if (second.Contains(key) == false)
                    {
                        throw new Exception($"Key \"{key}\" is missing from {second.Source}.");
                    }

                    Assert.That(second[key].ToString(), Is.EqualTo(first[key].ToString()), $"Values for {key} should be equal.");
                }
            }
        }
        private static Brush GetMarkBrush(ResourceDictionary dictionary)
        {
            Brush markBrush = null;

            if (dictionary.Contains(EditorFormatDefinition.ForegroundBrushId))
            {
                markBrush = dictionary[EditorFormatDefinition.ForegroundBrushId] as Brush;
            }
            else if (dictionary.Contains(EditorFormatDefinition.ForegroundColorId))
            {
                markBrush = new SolidColorBrush((Color)dictionary[EditorFormatDefinition.ForegroundColorId]);
            }
            else
            {
                markBrush = Brushes.Blue;
            }

            if (markBrush.CanFreeze)
            {
                markBrush.Freeze();
            }

            return(markBrush);
        }
Esempio n. 14
0
        private void OverrideFlyoutResources(ResourceDictionary resources, bool accent = false)
        {
            var fromColorKey = accent ? "MahApps.Colors.Highlight" : "MahApps.Colors.Flyout";

            resources.BeginInit();

            var fromColor = (Color)resources[fromColorKey];

            resources["MahApps.Colors.White"]  = fromColor;
            resources["MahApps.Colors.Flyout"] = fromColor;

            var newBrush = new SolidColorBrush(fromColor);

            newBrush.Freeze();
            resources["MahApps.Brushes.Flyout.Background"]  = newBrush;
            resources["MahApps.Brushes.Control.Background"] = newBrush;
            resources["MahApps.Brushes.White"]             = newBrush;
            resources["MahApps.Brushes.WhiteColor"]        = newBrush;
            resources["MahApps.Brushes.DisabledWhite"]     = newBrush;
            resources["MahApps.Brushes.Window.Background"] = newBrush;
            resources[SystemColors.WindowBrushKey]         = newBrush;

            if (accent)
            {
                fromColor = (Color)resources["MahApps.Colors.IdealForeground"];
                newBrush  = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["MahApps.Brushes.Flyout.Foreground"] = newBrush;
                resources["MahApps.Brushes.Text"]       = newBrush;
                resources["MahApps.Brushes.Label.Text"] = newBrush;

                if (resources.Contains("MahApps.Colors.AccentBase"))
                {
                    fromColor = (Color)resources["MahApps.Colors.AccentBase"];
                }
                else
                {
                    var accentColor = (Color)resources["MahApps.Colors.Accent"];
                    fromColor = Color.FromArgb(255, accentColor.R, accentColor.G, accentColor.B);
                }
                newBrush = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["MahApps.Colors.Highlight"]  = fromColor;
                resources["MahApps.Brushes.Highlight"] = newBrush; resources["MahApps.Brushes.Highlight"] = newBrush;
            }

            resources.EndInit();
        }
Esempio n. 15
0
        private void OverrideFlyoutResources(ResourceDictionary resources, bool accent = false)
        {
            var fromColorKey = accent ? "HighlightColor" : "FlyoutColor";

            resources.BeginInit();

            var fromColor = (Color)resources[fromColorKey];

            resources["WhiteColor"]  = fromColor;
            resources["FlyoutColor"] = fromColor;

            var newBrush = new SolidColorBrush(fromColor);

            newBrush.Freeze();
            resources["FlyoutBackgroundBrush"]     = newBrush;
            resources["ControlBackgroundBrush"]    = newBrush;
            resources["WhiteBrush"]                = newBrush;
            resources["WhiteColorBrush"]           = newBrush;
            resources["DisabledWhiteBrush"]        = newBrush;
            resources["WindowBackgroundBrush"]     = newBrush;
            resources[SystemColors.WindowBrushKey] = newBrush;

            if (accent)
            {
                fromColor = (Color)resources["IdealForegroundColor"];
                newBrush  = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["FlyoutForegroundBrush"] = newBrush;
                resources["TextBrush"]             = newBrush;
                resources["LabelTextBrush"]        = newBrush;

                if (resources.Contains("AccentBaseColor"))
                {
                    fromColor = (Color)resources["AccentBaseColor"];
                }
                else
                {
                    var accentColor = (Color)resources["AccentColor"];
                    fromColor = Color.FromArgb(255, accentColor.R, accentColor.G, accentColor.B);
                }
                newBrush = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["HighlightColor"] = fromColor;
                resources["HighlightBrush"] = newBrush;
            }

            resources.EndInit();
        }
Esempio n. 16
0
        public static ThemeInfo[] GetAllStyles()
        {
            string path     = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MediaPoint");
            string fileName = Path.Combine(path, @"Themes\");

            List <string> dirs = new List <string>();

            if (Directory.Exists(fileName) && HasPermission(fileName, FileSystemRights.Read))
            {
                dirs.AddRange(Directory.GetDirectories(fileName).Select(d => new DirectoryInfo(d).Name).ToArray());
            }

            path     = Assembly.GetExecutingAssembly().GetPath();
            fileName = Path.Combine(path, @"Themes\");

            if (Directory.Exists(fileName) && HasPermission(fileName, FileSystemRights.Read))
            {
                dirs.AddRange(Directory.GetDirectories(fileName).Select(d => new DirectoryInfo(d).Name).ToArray());
            }

            dirs = dirs.Distinct().ToList();

            List <ThemeInfo> ret = new List <ThemeInfo>();

            foreach (var themePath in dirs)
            {
                string themeFullPath;
                string themeFile;
                GetFullPath(Application.Current.GetType().Assembly.GetName().Name, themePath, out themeFullPath, out themeFile);

                string themeName;
                using (FileStream fs = new FileStream(themeFile, FileMode.Open, FileAccess.Read))
                {
                    ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
                    if (!dic.Contains(THEME_PREFIX))
                    {
                        throw new ThemeException("A theme for MediaPoint needs to have a TextBlock marked with x:Name=\"Theme\" while the text should be the name of the theme.");
                    }
                    themeName = ((TextBlock)dic[THEME_PREFIX]).Text;
                }

                ret.Add(new ThemeInfo {
                    Name = themeName, Path = themePath
                });
            }

            return(ret.ToArray());
        }
Esempio n. 17
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
        }
Esempio n. 18
0
        public static string String(string key, params object[] objs)
        {
            ResourceDictionary resDic = GetResourceDictionary();

            if (!resDic.Contains(key))
            {
                return(key); // TODO: what we should return here?
            }
            string ret = resDic[key] as string;

            if (ret == null)
            {
                return(key); // TODO: what we should return here?
            }
            return(System.String.Format(ret, objs).Replace(@"\n", Environment.NewLine));
        }
Esempio n. 19
0
 private object GetValueFromDictionary(string key, ResourceDictionary dictionary)
 {
     if (dictionary.Contains(key))
     {
         return(dictionary[key]);
     }
     foreach (var resourceDictionary in dictionary.MergedDictionaries)
     {
         var result = GetValueFromDictionary(key, resourceDictionary);
         if (result != null)
         {
             return(result);
         }
     }
     return(null);
 }
Esempio n. 20
0
        public static string String(string key)
        {
            ResourceDictionary resDic = GetResourceDictionary();

            if (!resDic.Contains(key))
            {
                return(key); // TODO: what we should return here?
            }
            string ret = resDic[key] as string;

            if (ret == null)
            {
                return(key); // TODO: what we should return here?
            }
            return(ret.Replace(@"\n", Environment.NewLine));
        }
Esempio n. 21
0
        public ThemeInfo LoadStyles(string appname, string styleName, ResourceDictionary appDic)
        {
            string path;
            string fileName;

            GetFullPath(appname, styleName, out path, out fileName);

            try
            {
                var themeName = "";
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
                    if (!dic.Contains(THEME_PREFIX))
                    {
                        throw new ThemeException("A theme for MediaPoint needs to have a TextBlock marked with x:Name=\"Theme\" while the text should be the name of the theme.");
                    }
                    themeName = ((TextBlock)dic[THEME_PREFIX]).Text;
                    if (PerformInit)
                    {
                        appDic.BeginInit();
                    }
                    var theme = Application.Current.Resources.MergedDictionaries.FirstOrDefault(rd =>
                    {
                        return(rd.Contains(THEME_PREFIX));
                    });
                    if (theme != null)
                    {
                        appDic.MergedDictionaries.Remove(theme);
                    }
                    appDic.MergedDictionaries.Add(dic);
                    if (PerformInit)
                    {
                        appDic.EndInit();
                    }
                    CurrentStyleFolder = Path.Combine(path, @"Themes\" + styleName);

                    return(new ThemeInfo {
                        Name = themeName, Path = styleName
                    });
                }
            }
            catch (Exception ex)
            {
                throw new ThemeException("Style: " + styleName + " contains an invalid WPF ResourceDictionary.\r\n\r\n" + ex.Message + "\r\n\r\n" + ex.StackTrace);
            }
        }
Esempio n. 22
0
        void UpdateSupports(ResourceDictionary skin)
        {
            if (skin.Contains("Supports"))
            {
                CurrentSupports = (string)skin["Supports"];
            }
            else
            {
                CurrentSupports = "";
            }

            // CheckSupports
            if (options != null)
            {
                options.Supports = CurrentSupports;
            }
        }
        private void FillResourceDic(string viewId, ResourceDictionary resourceDictionary)
        {
            if (!_dicByViewId.ContainsKey(viewId))
            {
                _dicByViewId.Add(viewId, resourceDictionary);
            }
            IList <ILangViewItem> langItems = LangViewItemSet.Instance.GetLangItems(VirtualRoot.Lang.GetId(), viewId);
            Type stringType = typeof(string);

            foreach (var item in langItems)
            {
                if (resourceDictionary.Contains(item.Key) && resourceDictionary[item.Key].GetType() == stringType && !string.IsNullOrEmpty(item.Value))
                {
                    resourceDictionary[item.Key] = item.Value;
                }
            }
        }
Esempio n. 24
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);
            }
        }
Esempio n. 25
0
        private void ComboBox_Theme_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_Theme.SelectedIndex == -1)
            {
                return;
            }
            String directory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            directory = System.IO.Path.Combine(directory, "Themes", "Sun");
            DirectoryInfo d = new DirectoryInfo(directory);
            String        filepath;
            String        imagepath;

            foreach (var file in d.GetFiles("*.xaml"))
            {
                filepath = System.IO.Path.Combine(directory, file.Name);
                var languageDictionary = new ResourceDictionary();
                languageDictionary.Source = new Uri(filepath);
                if (languageDictionary.Contains("ThemeName"))
                {
                    if (languageDictionary["ThemeName"].ToString() == ComboBox_Theme.SelectedItem.ToString())
                    {
                        switch (languageDictionary["Type"].ToString())
                        {
                        case "Image":
                            imagepath = System.IO.Path.Combine(directory, languageDictionary["ImageFileName"].ToString());
                            this.Sun_Rectangle_Bg.Fill = new ImageBrush(new BitmapImage(new Uri(imagepath)));
                            break;

                        case "Color":
                            bgColor.R          = byte.Parse(languageDictionary["BgColorR"].ToString());
                            bgColor.G          = byte.Parse(languageDictionary["BgColorG"].ToString());
                            bgColor.B          = byte.Parse(languageDictionary["BgColorB"].ToString());
                            Slider_Red.Value   = bgColor.R;
                            Slider_Green.Value = bgColor.G;
                            Slider_Blue.Value  = bgColor.B;
                            SetBg(bgColor);
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 26
0
        /// <summary>
        /// Returns a template
        /// </summary>
        /// <param name="source">Pass null to search entire app</param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static DataTemplate FindTemplate(object source, string key)
        {
            var                fe = source as FrameworkElement;
            object             obj;
            ResourceDictionary rd = fe != null ? fe.Resources : App.Current.Resources;

            if (rd.Contains(key))
            {
                obj = rd[key];
                DataTemplate dt = obj as DataTemplate;
                if (dt != null)
                {
                    return(dt);
                }
            }
            return(null);
        }
Esempio n. 27
0
        public static void AdjustResourceColors(ResourceDictionary resources)
        {
            const float factor = 1.08F;

            AdjustResourceColor(NormalKey, NormalSelectedKey);
            AdjustResourceColor(AvailableKey, AvailableSelectedKey);
            AdjustResourceColor(ConnectedKey, ConnectedSelectedKey);

            void AdjustResourceColor(string sourceKey, string targetKey)
            {
                if (resources.Contains(sourceKey))
                {
                    var sourceColor = (Color)resources[sourceKey];
                    resources[targetKey] = sourceColor.ToBrightened(factor);
                }
            }
        }
Esempio n. 28
0
        public static void AddString(string key, string value, string language, bool isOverWrite = true)
        {
            string             requestedLanguage  = string.Format(@"Language\StringResource.{0}.xaml", language);
            ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));

            if (resourceDictionary.Contains(key))
            {
                if (isOverWrite)
                {
                    resourceDictionary[key] = value;
                }
            }
            else
            {
                resourceDictionary.Add(key, value);
            }
        }
Esempio n. 29
0
        private static (Style style, ResourceDictionary resource) FindStyle(ResourceDictionary resource, string styleKey)
        {
            if (resource.Contains(styleKey))
            {
                return(resource[styleKey] as Style, resource);
            }

            foreach (var item in resource.MergedDictionaries)
            {
                var result = FindStyle(item, styleKey);
                if (result.style != null)
                {
                    return(result);
                }
            }
            return(null, null);
        }
Esempio n. 30
0
        protected virtual void OnChildAdded(IPlotterElement child)
        {
            if (child != null)
            {
                addingElements.Push(child);
                try
                {
                    UIElement visualProxy = CreateVisualProxy(child);
                    visualBindingCollection.Cache.Add(child, visualProxy);

                    if (child.Plotter != null)
                    {
                        throw new InvalidOperationException(Strings.Exceptions.PlotterElementAddedToAnotherPlotter);
                    }

                    FrameworkElement styleableElement = child as FrameworkElement;
                    if (styleableElement != null)
                    {
                        Type key = styleableElement.GetType();
                        if (genericResources.Contains(key))
                        {
                            Style elementStyle = (Style)genericResources[key];
                            styleableElement.Style = elementStyle;
                        }
                    }

                    //if (onLoadedCalled)
                    //{
                    child.OnPlotterAttached(this);
                    if (child.Plotter != this)
                    {
                        throw new InvalidOperationException(Strings.Exceptions.InvalidParentPlotterValue);
                    }
                    //}
                    //else
                    //{
                    //    elementsWaitingForBeingAttached.Enqueue(child);
                    //}
                }
                finally
                {
                    addingElements.Pop();
                }
            }
        }