/// <summary>
        /// Returns the localized value.
        /// </summary>
        /// <returns></returns>
        object ProduceValue(Type targetPropertyType, bool dataBound, object dataBoundValueOrValues)
        {
            Debug.Assert(targetPropertyType != null);

            var rootObject = this.RootObject;

            if (rootObject == null)
            {
                // The object has been GC
                return(TypeUtils.GetDefaultValue(targetPropertyType));
            }

            var dispatcher = rootObject.Dispatcher;

            var cultureInfo     = LocalizationScope.GetCulture(rootObject) ?? dispatcher.Thread.CurrentCulture;
            var uiCultureInfo   = LocalizationScope.GetUICulture(rootObject) ?? dispatcher.Thread.CurrentUICulture;
            var resourceManager = ResourceManagerHelper.GetResourceManager(rootObject);

            return(base.ProduceValue(
                       null,
                       targetPropertyType,
                       resourceManager,
                       cultureInfo,
                       uiCultureInfo,
                       dataBound,
                       dataBoundValueOrValues
                       ));
        }
        /// <summary>
        /// Gets the UI culture set for the object.
        /// </summary>
        /// <returns>
        /// A <see cref="CultureInfo"/> or null if no explicit value is set for the object.
        /// </returns>
        public CultureInfo GetUICulture()
        {
            var obj = Object;

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

            CultureInfo result;

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

            if (result == null)
            {
                // Get the culture of the UI thread in case the current thread is different
                result = obj.Dispatcher.Thread.CurrentUICulture;
            }

            return(result);
        }
        /// <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);
        }
        object ProduceValue(bool dataBound, object dataBoundValueOrValues)
        {
            var targetObject = this.TargetObject;

            if (targetObject == null)
            {
                // The object has been GC
                return(TargetProperty.DefaultValue);
            }

            var dispatcher      = targetObject.Dispatcher;
            var culture         = LocalizationScope.GetCulture(targetObject) ?? dispatcher.Thread.CurrentCulture;
            var uiCulture       = LocalizationScope.GetUICulture(targetObject) ?? dispatcher.Thread.CurrentUICulture;
            var resourceManager = ResourceManagerHelper.GetResourceManager(targetObject);

            return(base.ProduceValue(
                       TargetProperty,
                       TargetProperty.PropertyType,
                       resourceManager,
                       culture,
                       uiCulture,
                       dataBound,
                       dataBoundValueOrValues
                       ));
        }
        /// <summary>
        /// Returns the specified resource based on the current culture of the specified object.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="resourceKey"></param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// This method respects the resources and culture set via the <see cref="LocalizationScope.ResourceManager"/>
        /// and <see cref="LocalizationScope.UICulture"/> attached properties when retrieving the resource.
        /// </para>
        /// <para>
        /// This method is thread-safe.
        /// </para>
        /// </remarks>
        public static object GetResource(this DependencyObject obj, string resourceKey)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var dispatcher    = obj.Dispatcher;
            var uiCultureInfo = LocalizationScope.GetUICulture(obj) ?? dispatcher.Thread.CurrentUICulture;

            var resourceManager = ResourceManagerHelper.GetResourceManager(obj);

            if (resourceManager == null)
            {
                return(null);
            }
            else
            {
                return(resourceManager.GetObject(resourceKey, uiCultureInfo));
            }
        }
Exemple #6
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);
        }