/// <summary>
        /// Gets the <see cref="ResourceManager"/> set for the object.
        /// </summary>
        /// <returns>
        /// A <see cref="ResourceManager"/> or null if no explicit value is set for the object.
        /// </returns>
        public ResourceManager GetResourceManager()
        {
            var obj = Object;

            if (obj == null)
            {
                return(null);
            }

            ResourceManager result;

            if (obj.CheckAccess())
            {
                result = LocalizationScope.GetResourceManager(obj);
            }
            else
            {
                result = (ResourceManager)obj.Dispatcher.Invoke(new DispatcherOperationCallback(x => LocalizationScope.GetResourceManager((DependencyObject)x)), obj);
            }

            if (result == null)
            {
                result = LocalizationManager.DefaultResourceManager;
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Determines the <see cref="ResourceManager"/> to use for the specified <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">The method is not called on the UI thread of the specified <see cref="DependencyObject"/>.</exception>
        public static ResourceManager GetResourceManager(DependencyObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            obj.VerifyAccess();

            var resourceManager = LocalizationScope.GetResourceManager(obj);

            if (resourceManager == null)
            {
                if (DesignerProperties.GetIsInDesignMode(obj))
                {
                    // Window.GetWindow returns "null" at design time
                    return(GetDefaultResourceManagerForAssembly(DesignTimeHelper.GetDesignTimeAssembly()));
                }
                else
                {
                    var window = Window.GetWindow(obj);

                    if (window != null)
                    {
                        var localValue = window.ReadLocalValue(DefaultResourceManagerProperty);
                        if (localValue == DependencyProperty.UnsetValue)
                        {
                            resourceManager = GetDefaultResourceManagerForAssembly(window.GetType().Assembly);
                            window.SetValue(DefaultResourceManagerProperty, resourceManager);
                        }
                        else
                        {
                            resourceManager = localValue as ResourceManager;
                        }
                    }
                }
            }

            return(resourceManager);
        }