コード例 #1
0
        /// <summary>
        /// Executes the handler immediately if the element is loaded, otherwise wires it to the Loaded event.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="handler">The handler.</param>
        /// <returns>true if the handler was executed immediately; false otherwise</returns>
        public static bool ExecuteOnLoad(System.Windows.FrameworkContentElement element, RoutedEventHandler handler)
        {
#if SILVERLIGHT
            if ((bool)element.GetValue(IsLoadedProperty))
            {
#elif WinRT
            if (IsElementLoaded(element))
            {
#else
            if (element.IsLoaded)
            {
#endif
                handler(element, new RoutedEventArgs());
                return(true);
            }

            RoutedEventHandler loaded = null;
            loaded = (s, e) => {
                element.Loaded -= loaded;
#if SILVERLIGHT
                element.SetValue(IsLoadedProperty, true);
#endif
                handler(s, e);
            };
            element.Loaded += loaded;
            return(false);
        }