Esempio n. 1
0
        /// <summary>
        /// Searches for a resource with the specified key, and returns that resource, if found and of type specified type.
        /// </summary>
        /// <typeparam name="T">The return type of the resource.</typeparam>
        /// <param name="application">The <see cref="Application" /> to search in.</param>
        /// <param name="key">The name of the resource to find.</param>
        /// <returns>
        /// The found resource, or <see langword="default" />(<typeparamref name="T" />), if no resource with the provided key is found or it does not match the specified type.
        /// </returns>
        public static T TryFindResource <T>(this Application application, object key)
        {
            Check.ArgumentNull(application, nameof(application));
            Check.ArgumentNull(key, nameof(key));

            return(CSharp.CastOrDefault <T>(application.TryFindResource(key)));
        }
        /// <summary>
        /// Searches for a resource with the specified key, and returns that resource, if found and of type specified type.
        /// </summary>
        /// <typeparam name="T">The return type of the resource.</typeparam>
        /// <param name="frameworkElement">The <see cref="FrameworkElement" /> to search in.</param>
        /// <param name="key">The key identifier of the resource to be found.</param>
        /// <returns>
        /// The found resource, or <see langword="default" />(<typeparamref name="T" />), if no resource with the provided key is found or it does not match the specified type.
        /// </returns>
        public static T TryFindResource <T>(this FrameworkElement frameworkElement, object key)
        {
            Check.ArgumentNull(frameworkElement, nameof(frameworkElement));
            Check.ArgumentNull(key, nameof(key));

            return(CSharp.CastOrDefault <T>(frameworkElement.TryFindResource(key)));
        }
Esempio n. 3
0
        /// <summary>
        /// Method that can be used by the <see langword="get" /> accessor of a property. Backing fields are managed automatically.
        /// <para>Example: <see langword="public" /> <see cref="int" /> Foo { <see langword="get" /> => Get(() => Foo, () => 1); <see langword="set" /> => Set(() => Foo, <see langword="value" />); }</para>
        /// </summary>
        /// <typeparam name="T">The type of the property.</typeparam>
        /// <param name="property">The strongly typed lambda expression of the property.</param>
        /// <param name="defaultValue">A <see cref="Func{TResult}" /> that retrieves a default value. This delegate can be used as a property initializer. <paramref name="defaultValue" /> is invoked, if the property is retrieved for the first time and is not set.</param>
        /// <returns>
        /// The value of the property backing field. The default value is <see langword="default" />(<typeparamref name="T" />).
        /// </returns>
        protected T Get <T>(Expression <Func <T> > property, Func <T> defaultValue)
        {
            Check.ArgumentNull(property, nameof(property));

            string propertyName = property.GetMemberName();

            if (!BackingFields.ContainsKey(propertyName))
            {
                BackingFields[propertyName] = defaultValue == null ? default : defaultValue();
            }

            return(CSharp.CastOrDefault <T>(BackingFields[propertyName]));
        }
Esempio n. 4
0
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TResult result = Convert(CSharp.CastOrDefault <TValue>(value));

            return(Then == null ? result : Then.Convert(result, targetType, ThenParameter, culture));
        }
Esempio n. 5
0
 /// <summary>
 /// Compares two objects and returns an indication of their relative sort order. Specified <see cref="object" /> parameters that are not of the specified type are treated as <see langword="null" />.
 /// </summary>
 /// <param name="x">An <see cref="object" /> to compare to <paramref name="y" />.</param>
 /// <param name="y">An <see cref="object" /> to compare to <paramref name="x" />.</param>
 /// <returns>
 /// A signed integer that indicates the relative values of <paramref name="x" /> and <paramref name="y" />.
 /// </returns>
 public int Compare(object x, object y)
 {
     return(Compare(CSharp.CastOrDefault <T>(x), CSharp.CastOrDefault <T>(y)));
 }