Beispiel #1
0
        /// <summary>
        /// Remove the adorner from the adorner layer and let it be garbage collected.
        /// </summary>
        private void RemoveAdorner()
        {
            //
            // Stop the timer that might be about to fade out the adorner.
            //
            closeAdornerTimer.Stop();

            if (this.adornerLayer != null && this.adorner != null)
            {
                this.adornerLayer.Remove(this.adorner);
                this.adorner.DisconnectChild();
            }

            this.adorner      = null;
            this.adornerLayer = null;

            //
            // Ensure that the state of the adorned control reflects that
            // the the adorner is no longer.
            //
            this.IsAdornerVisible = false;
            this.adornerShowState = AdornerShowState.Hidden;
        }
Beispiel #2
0
        /// <summary>
        /// Internal method to show the adorner.
        /// </summary>
        private void ShowAdornerInternal()
        {
            if (this.adorner != null)
            {
                // Already adorned.
                return;
            }

            if (this.AdornerContent != null)
            {
                if (this.adornerLayer == null)
                {
                    this.adornerLayer = AdornerLayer.GetAdornerLayer(this);
                }

                if (this.adornerLayer != null)
                {
                    FrameworkElement adornedControl = this; // The control to be adorned defaults to 'this'.

                    if (!string.IsNullOrEmpty(this.AdornedTemplatePartName))
                    {
                        //
                        // If 'AdornedTemplatePartName' is set to a valid string then search the visual-tree
                        // for a UI element that has the specified part name.  If we find it then use it as the
                        // adorned control, otherwise throw an exception.
                        //
                        adornedControl = FindNamedChild(this, this.AdornedTemplatePartName);
                        if (adornedControl == null)
                        {
                            throw new ApplicationException("Failed to find a FrameworkElement in the visual-tree with the part name '" + this.AdornedTemplatePartName + "'.");
                        }
                    }

                    this.adorner = new FrameworkElementAdorner(this.AdornerContent, adornedControl,
                                                               this.HorizontalAdornerPlacement, this.VerticalAdornerPlacement,
                                                               this.AdornerOffsetX, this.AdornerOffsetY);
                    this.adornerLayer.Add(this.adorner);

                    UpdateAdornerDataContext();
                }
            }

            this.adornerShowState = AdornerShowState.Visible;
        }
Beispiel #3
0
        /// <summary>
        /// Internal method to hide the adorner.
        /// </summary>
        private void HideAdornerInternal()
        {
            if (this.adornerLayer == null || this.adorner == null)
            {
                // Not already adorned.
                return;
            }

            //
            // Stop the timer that might be about to fade out the adorner.
            //
            closeAdornerTimer.Stop();
            this.adornerLayer.Remove(this.adorner);
            this.adorner.DisconnectChild();

            this.adorner = null;
            this.adornerLayer = null;

            //
            // Ensure that the state of the adorned control reflects that
            // the the adorner is no longer.
            //
            this.adornerShowState = AdornerShowState.Hidden;
        }
Beispiel #4
0
        /// <summary>
        /// Remove the _adorner from the _adorner layer and let it be garbage collected.
        /// </summary>
        private void RemoveAdorner()
        {
            //
            // Stop the timer that might be about to fade out the _adorner.
            //
            closeAdornerTimer.Stop();

            if (this._adornerLayer != null && this._adorner != null)
            {
                this._adornerLayer.Remove(this._adorner);
                this._adorner.DisconnectChild();
            }

            this._adorner = null;
            this._adornerLayer = null;

            //
            // Ensure that the state of the adorned control reflects that
            // the the _adorner is no longer.
            //
            this.IsAdornerVisible = false;
            this._adornerShowState = AdornerShowState.Hidden;
        }
Beispiel #5
0
        /// <summary>
        /// Instance the _adorner and add it to the _adorner layer.
        /// </summary>
        private void AddAdorner()
        {
            if (this.AdornerContent != null)
            {
                if (this._adornerLayer == null)
                {
                    this._adornerLayer = AdornerLayer.GetAdornerLayer(this);
                }

                if (this._adornerLayer != null)
                {
                    FrameworkElement adornedControl = this; // The control to be adorned defaults to 'this'.

                    if (!string.IsNullOrEmpty(this.AdornedTemplatePartName))
                    {
                        //
                        // If 'AdornedTemplatePartName' is set to a valid string then search the visual-tree
                        // for a UI element that has the specified part name.  If we find it then use it as the
                        // adorned control, otherwise throw an exception.
                        //
                        adornedControl = FindNamedChild(this, this.AdornedTemplatePartName);
                        if (adornedControl == null)
                        {
                            throw new ApplicationException("Failed to find a FrameworkElement in the visual-tree with the part name '" + this.AdornedTemplatePartName + "'.");
                        }
                    }

                    this._adorner = new FrameworkElementAdorner(this.AdornerContent, adornedControl,
                                                               this.HorizontalAdornerPlacement, this.VerticalAdornerPlacement,
                                                               this.AdornerOffsetX, this.AdornerOffsetY);
                    _adornerLayer.Add(this._adorner);

                    //
                    // Update the layout of the _adorner layout so that clients that depend
                    // on the 'AdornerShown' event can use the visual tree of the _adorner.
                    //
                    _adornerLayer.UpdateLayout();

                    UpdateAdornerDataContext();
                }
            }
        }
        /// <summary>
        /// Invokes the action.
        /// </summary>
        /// <param name="parameter">The parameter to the action. If the Action does not require a parameter, the parameter may be set to a null reference.</param>
        protected override void Invoke(object parameter)
        {
            // See if we were passed a parameter indicating whether to run our action or reverse the action.
            // Assume to run if not supplied.
            bool isThemeEnabled;

            if (parameter == null || !Boolean.TryParse(parameter.ToString(), out isThemeEnabled))
            {
                isThemeEnabled = true;
            }

            // Reversing theme? Remove resources.
            if (!isThemeEnabled)
            {
                this.AssociatedObject.Resources.MergedDictionaries.Clear();
                return;
            }

            // Running action -- no theme supplied? Nothing to do.
            if (this.ThemeUri == null)
            {
                return;
            }

            // See if the element we're applied to is loaded.  If not, we can't do our theme switch
            // here, instead we need to wait until it's loaded.
            if (!this.AssociatedObject.IsLoaded)
            {
                // Hook up a temporary handler
                RoutedEventHandler waitForLoad = null;
                waitForLoad = (s, e) => {
                    this.AssociatedObject.Loaded -= waitForLoad;
                    this.Invoke(isThemeEnabled);
                };
                this.AssociatedObject.Loaded += waitForLoad;

                return;
            }

            // Apply the theme.
            try
            {
                ResourceDictionary rd = Application.LoadComponent(this.ThemeUri) as ResourceDictionary;
                if (rd != null)
                {
                    // See if we have content, and it's a UIElement type -- if so, we will try to gradually
                    // fade the content for a nice effect.  We also require an Adorner Layer to be present
                    // in the Window's control template
                    UIElement    elementContent = this.AssociatedObject.Content as UIElement;
                    AdornerLayer adornerLayer   = AdornerLayer.GetAdornerLayer(elementContent ?? this.AssociatedObject);
                    if (elementContent == null || adornerLayer == null)
                    {
                        // No layer, just replace the theme.
                        this.AssociatedObject.Resources.MergedDictionaries.Clear();
                        this.AssociatedObject.Resources.MergedDictionaries.Add(rd);
                        return;
                    }

                    int width, height;
                    if (elementContent is FrameworkElement)
                    {
                        FrameworkElement fe = (FrameworkElement)elementContent;
                        width  = (int)fe.ActualWidth;
                        height = (int)fe.ActualHeight;
                    }
                    else
                    {
                        width  = (int)(this.AssociatedObject.ActualWidth - SystemParameters.BorderWidth * 2);
                        height = (int)(this.AssociatedObject.ActualHeight - SystemParameters.CaptionHeight);
                    }

                    // Capture a bitmap of the current screen.
                    var currentContent     = (Visual)VisualTreeHelper.GetChild(this.AssociatedObject, 0);
                    RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
                    rtb.Render(currentContent);

                    var staticImage = new Image {
                        Source = rtb, Stretch = Stretch.None, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top
                    };
                    var imageAdorner = new FrameworkElementAdorner(elementContent, staticImage);
                    adornerLayer.Add(imageAdorner);

                    // Merge the resources in.
                    this.AssociatedObject.Resources.MergedDictionaries.Clear();
                    this.AssociatedObject.Resources.MergedDictionaries.Add(rd);

                    // Now animate the opacity of the image out so we can see the real controls
                    // underneath, then drop the image off.
                    DoubleAnimation doubleAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromMilliseconds(1500)));
                    doubleAnimation.Completed += (s, e) =>
                    {
                        adornerLayer.Remove(imageAdorner);
                        this.RaiseThemeChanged();
                    };
                    staticImage.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// Internal method to show the adorner.
        /// </summary>
        private void ShowAdornerInternal()
        {
            if (_adorner != null)
            {
                // Already adorned.
                return;
            }

            if (AdornerContent != null)
            {
                if (_adornerLayer == null)
                {
                    _adornerLayer = AdornerLayer.GetAdornerLayer(this);
                }
                //todo investigate why is null in dialog view
                if (_adornerLayer == null)
                {
                    return;
                }

                _adorner = new FrameworkElementAdorner(AdornerContent, this, HorizontalAdornerPlacement, VerticalAdornerPlacement, AdornerOffsetX, AdornerOffsetY);
                _adornerLayer.Add(_adorner);
                UpdateAdornerDataContext();
                //_adorner.Visibility = Visibility.Visible;

            }
        }
        /// <summary>
        /// Internal method to hide the adorner.
        /// </summary>
        private void HideAdornerInternal()
        {
            if (_adornerLayer == null || _adorner == null)
            {
                // Not already adorned.
                return;
            }

            _adornerLayer.Remove(_adorner);
            _adorner.DisconnectChild();
            //_adorner.Visibility = Visibility.Hidden;

            _adorner = null;
            _adornerLayer = null;
        }