/// <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
                       ));
        }
예제 #2
0
        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
                       ));
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var type = Type;

            if (type == null)
            {
                if (string.IsNullOrEmpty(AssemblyName) || string.IsNullOrEmpty(ResourceFile))
                {
                    throw new InvalidOperationException($"Either {nameof(Type)} or {nameof(AssemblyName)} and {nameof(ResourceFile)} must be specified.");
                }
                else
                {
                    var assembly = AppDomain.CurrentDomain.Load(AssemblyName);
                    return(ResourceManagerHelper.GetResourceManager(assembly, ResourceFile)
                           ?? throw new InvalidOperationException($"Resource file named '{ResourceFile}' was not found in '{AssemblyName}'. Make sure the full name of the resource file is specified (including namespace and file name)."));
                }
            }
            else
            {
                return(ResourceManagerHelper.GetResourceManager(type)
                       ?? throw new InvalidOperationException($"Resource file corresponding to type '{type.FullName}' was not found."));
            }
        }
        /// <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));
            }
        }