2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
21 namespace Tizen.NUI.Binding
23 internal static class ResourcesExtensions
25 public static IEnumerable<KeyValuePair<string, object>> GetMergedResources(this IElement element)
27 Dictionary<string, object> resources = null;
28 while (element != null)
30 var ve = element as IResourcesProvider;
31 if (ve != null && ve.IsResourcesCreated)
33 resources = resources ?? new Dictionary<string, object>();
34 if (null == resources)
39 if (ve.XamlResources != null)
41 foreach (KeyValuePair<string, object> res in ve.XamlResources.MergedResources)
42 if (!resources.ContainsKey(res.Key))
43 resources.Add(res.Key, res.Value);
47 var app = element as Application;
48 if (app != null && app.SystemResources != null)
50 resources = resources ?? new Dictionary<string, object>(8);
51 if (null == resources)
56 foreach (KeyValuePair<string, object> res in app.SystemResources)
57 if (!resources.ContainsKey(res.Key))
58 resources.Add(res.Key, res.Value);
60 element = element.Parent;
65 public static bool TryGetResource(this IElement element, string key, out object value)
67 while (element != null)
69 var ve = element as IResourcesProvider;
70 if (ve != null && ve.IsResourcesCreated && ve.XamlResources != null && ve.XamlResources.TryGetValue(key, out value))
74 var app = element as Application;
75 if (app != null && app.SystemResources != null && app.SystemResources.TryGetValue(key, out value))
79 element = element.Parent;
82 //Fallback for the XF previewer
83 if (Application.Current != null && ((IResourcesProvider)Application.Current).IsResourcesCreated && Application.Current.XamlResources.TryGetValue(key, out value))