Exemplo n.º 1
0
        /// <summary>
        /// Gets the full url of a relative url to the current host.
        /// </summary>
        /// <param name="relativeUrl">The relative URL.</param>
        /// <returns>The host url.</returns>
        /// <exception cref="ArgumentException">The <paramref name="relativeUrl"/> is <c>null</c> or whitespace.</exception>
        public static string GetHostUrl(string relativeUrl)
        {
            Argument.IsNotNullOrWhitespace("relativeUrl", relativeUrl);

            if (!relativeUrl.StartsWith("/"))
            {
                relativeUrl = "/" + relativeUrl;
            }

            var source = Application.Current.Host.Source;

            string hostXapSource = source.OriginalString;
            string hostXapFile   = source.AbsolutePath;

            string host  = hostXapSource;
            int    index = hostXapSource.LastIndexOf(hostXapFile);

            if (index > 0)
            {
                host = hostXapSource.Substring(0, index);
            }

            return(string.Format("{0}{1}", host, relativeUrl));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the string from the specified resource file.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <returns>System.String.</returns>
        /// <exception cref="ArgumentException">The <paramref name="resourceName" /> is <c>null</c> or whitespace.</exception>
        public static string GetString(string resourceName)
        {
            Argument.IsNotNullOrWhitespace("resourceName", resourceName);

            return(_languageService.GetString(resourceName));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the string from the specified resource file.
        /// </summary>
        /// <param name="callingType">Type of the calling.</param>
        /// <param name="resourceFile">The resource file.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentException">The <paramref name="resourceFile"/> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ArgumentException">The <paramref name="resourceName"/> is <c>null</c> or whitespace.</exception>
        public static string GetString(Type callingType, string resourceFile, string resourceName)
        {
            Argument.IsNotNullOrWhitespace("resourceName", resourceName);

            return(GetString(resourceName));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a command using a naming convention with the specified gesture.
        /// </summary>
        /// <param name="commandManager">The command manager.</param>
        /// <param name="containerType">Type of the container.</param>
        /// <param name="commandNameFieldName">Name of the command name field.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="commandManager"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="containerType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="commandNameFieldName"/> is <c>null</c>.</exception>
        public static void CreateCommandWithGesture(this ICommandManager commandManager, Type containerType, string commandNameFieldName)
        {
            Argument.IsNotNull(() => commandManager);
            Argument.IsNotNull(() => containerType);
            Argument.IsNotNullOrWhitespace(() => commandNameFieldName);

            Log.Debug("Creating command '{0}'", commandNameFieldName);

            var commandNameField = containerType.GetFieldEx(commandNameFieldName, BindingFlags.Public | BindingFlags.Static);

            if (commandNameField == null)
            {
                Log.ErrorAndThrowException <InvalidOperationException>("Command '{0}' is not available on container type '{1}'",
                                                                       commandNameFieldName, containerType.GetSafeFullName());
            }

            var commandName = (string)commandNameField.GetValue(null);

            if (commandManager.IsCommandCreated(commandName))
            {
                Log.Debug("Command '{0}' is already created, skipping...", commandName);
                return;
            }

            InputGesture commandInputGesture = null;
            var          inputGestureField   = containerType.GetFieldEx(string.Format("{0}InputGesture", commandNameFieldName),
                                                                        BindingFlags.Public | BindingFlags.Static);

            if (inputGestureField != null)
            {
                commandInputGesture = inputGestureField.GetValue(null) as InputGesture;
            }

            commandManager.CreateCommand(commandName, commandInputGesture);

            var commandContainerName = string.Format("{0}CommandContainer", commandName.Replace(".", string.Empty));

            var commandContainerType = (from type in TypeCache.GetTypes()
                                        where string.Equals(type.Name, commandContainerName, StringComparison.OrdinalIgnoreCase)
                                        select type).FirstOrDefault();

            if (commandContainerType != null)
            {
                Log.Debug("Found command container '{0}', registering it in the ServiceLocator now", commandContainerType.GetSafeFullName());

                var serviceLocator = commandManager.GetServiceLocator();
                if (!serviceLocator.IsTypeRegistered(commandContainerType))
                {
                    var typeFactory      = serviceLocator.ResolveType <ITypeFactory>();
                    var commandContainer = typeFactory.CreateInstance(commandContainerType);
                    if (commandContainer != null)
                    {
                        serviceLocator.RegisterInstance(commandContainer);
                    }
                    else
                    {
                        Log.Warning("Cannot create command container '{0}', skipping registration", commandContainerType.GetSafeFullName());
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DisplayNameAttribute"/> class.
        /// </summary>
        /// <param name="displayName">
        /// The display name.
        /// </param>
        /// <exception cref="ArgumentException">If <paramref name="displayName"/> is <c>null</c> or a whitespace.</exception>
        public DisplayNameAttribute(string displayName)
        {
            Argument.IsNotNullOrWhitespace("displayName", displayName);

            DisplayName = displayName;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a command using a naming convention with the specified gesture.
        /// </summary>
        /// <param name="commandManager">The command manager.</param>
        /// <param name="containerType">Type of the container.</param>
        /// <param name="commandNameFieldName">Name of the command name field.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="commandManager"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="containerType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="commandNameFieldName"/> is <c>null</c>.</exception>
        public static void CreateCommandWithGesture(this ICommandManager commandManager, Type containerType, string commandNameFieldName)
        {
            Argument.IsNotNull("commandManager", commandManager);
            Argument.IsNotNull("containerType", containerType);
            Argument.IsNotNullOrWhitespace("commandNameFieldName", commandNameFieldName);

            Log.Debug("Creating command '{0}'", commandNameFieldName);

            // Note: we must store bindingflags inside variable otherwise invalid IL will be generated
            var bindingFlags     = BindingFlags.Public | BindingFlags.Static;
            var commandNameField = containerType.GetFieldEx(commandNameFieldName, bindingFlags);

            if (commandNameField is null)
            {
                throw Log.ErrorAndCreateException <InvalidOperationException>("Command '{0}' is not available on container type '{1}'",
                                                                              commandNameFieldName, containerType.GetSafeFullName(false));
            }

            var commandName = (string)commandNameField.GetValue(null);

            if (commandManager.IsCommandCreated(commandName))
            {
                Log.Debug("Command '{0}' is already created, skipping...", commandName);
                return;
            }

            InputGesture commandInputGesture = null;
            var          inputGestureField   = containerType.GetFieldEx(string.Format("{0}InputGesture", commandNameFieldName), bindingFlags);

            if (inputGestureField != null)
            {
                commandInputGesture = inputGestureField.GetValue(null) as InputGesture;
            }

            commandManager.CreateCommand(commandName, commandInputGesture);

            var commandContainerName = string.Format("{0}CommandContainer", commandName.Replace(".", string.Empty));

            // https://github.com/Catel/Catel/issues/1383: CommandManager.CreateCommandWithGesture does not create CommandContainer
            var commandContainerType = (from type in TypeCache.GetTypes(allowInitialization: true)
                                        where string.Equals(type.Name, commandContainerName, StringComparison.OrdinalIgnoreCase)
                                        select type).FirstOrDefault();

            if (commandContainerType is null)
            {
                Log.Debug("Couldn't find command container '{0}', you will need to add a custom action or command manually in order to make the CompositeCommand useful", commandContainerName);
                return;
            }

            Log.Debug("Found command container '{0}', registering it in the ServiceLocator now", commandContainerType.GetSafeFullName(false));

            var serviceLocator = commandManager.GetServiceLocator();

            if (!serviceLocator.IsTypeRegistered(commandContainerType))
            {
                var typeFactory      = serviceLocator.ResolveType <ITypeFactory>();
                var commandContainer = typeFactory.CreateInstance(commandContainerType);
                if (commandContainer != null)
                {
                    serviceLocator.RegisterInstance(commandContainerType, commandContainer);
                }
                else
                {
                    Log.Warning("Cannot create command container '{0}', skipping registration", commandContainerType.GetSafeFullName(false));
                }
            }
        }