示例#1
0
        internal static void OnVisualChanged(Object sender, DependencyPropertyChangedEventArgs e)
        {
            Viewport2DVisual3D viewport2DVisual3D = ((Viewport2DVisual3D)sender);

            // remove the old parent, add on a new one
            Visual oldValue = (Visual)e.OldValue;
            Visual newValue = (Visual)e.NewValue;

            if (oldValue != newValue)
            {
                //
                // The following code deals with properly setting up the new child to have its inheritance context
                // only point towards this Viewport2DVisual3D.
                //
                // When we add the new visual as a child, if that visual is an FE (which most will be) we expect it to
                // clear the inheritance context (IC) since it has a visual parent.  In the case of a non-FE they don't
                // deal with ICs anyway, so they should have a null IC.  The Assert that follows then guards against
                // the child not having a null inheritance context.
                //
                // We then set the target Visual on the internal brush to be this new visual.  Since when we created
                // the brush we set it to not be an inheritance context, the InheritanceContext should still be null.
                //
                // We become the IC after returning from this function, since the function that calls this change handler
                // will set us as the IC.
                //

                if (viewport2DVisual3D.CacheMode as BitmapCache != null)
                {
                    viewport2DVisual3D.InternalBitmapCacheBrush.Target = newValue;
                    Debug.Assert((newValue == null || newValue.InheritanceContext == null),
                                 "Expected BitmapCacheBrush to remove the InheritanceContext on newValue");
                }
                else
                {
                    // Add ourselves as the parent of the object
                    viewport2DVisual3D.RemoveVisualChild(oldValue);
                    viewport2DVisual3D.AddVisualChild(newValue);

                    Debug.Assert((newValue == null || newValue.InheritanceContext == null),
                                 "Expected AddVisualChild to remove the InheritanceContext on newValue");

                    // Change the brush's target
                    viewport2DVisual3D.InternalVisualBrush.Visual = newValue;

                    // setting the visual brush to use this new child should not invalidate our previous condition
                    Debug.Assert((newValue == null || newValue.InheritanceContext == null),
                                 "Expected the InternalVisualBrush not to set the InheritanceContext");
                }
            }
        }
示例#2
0
        internal static void OnCacheModeChanged(Object sender, DependencyPropertyChangedEventArgs e)
        {
            Viewport2DVisual3D viewport2DVisual3D = ((Viewport2DVisual3D)sender);

            BitmapCache oldValue = (CacheMode)e.OldValue as BitmapCache;
            BitmapCache newValue = (CacheMode)e.NewValue as BitmapCache;

            if (oldValue != newValue)
            {
                viewport2DVisual3D.InternalBitmapCacheBrush.BitmapCache = newValue;

                //
                // The BitmapCacheBrush doesn't point directly at the Visual like the VisualBrush does,
                // since BitmapCacheBrush ignores most properties on a Visual by design.  In order for
                // those properties to be respected to match the internal VisualBrush's behavior, we insert
                // a dummy Visual node between the V2DV3D and its 2D Visual.  We then target the dummy
                // node with the brush instead.
                //

                if (oldValue == null)
                {
                    //
                    // If we are swapping from using the VisualBrush to using the BitmapCacheBrush...
                    //

                    // Remove the visual child from the V2DV3D and add the dummy child.
                    viewport2DVisual3D.RemoveVisualChild(viewport2DVisual3D.Visual);
                    viewport2DVisual3D.AddVisualChild(viewport2DVisual3D.InternalBitmapCacheBrush.InternalTarget);

                    Debug.Assert((viewport2DVisual3D.InternalBitmapCacheBrush.InternalTarget == null ||
                                  viewport2DVisual3D.InternalBitmapCacheBrush.InternalTarget.InheritanceContext == null),
                                 "Expected AddVisualChild to remove the InheritanceContext on InternalTarget");

                    // Swap the brush pointing to the visual.  The cache brush will re-parent the visual to the dummy.
                    viewport2DVisual3D.InternalVisualBrush.Visual      = null;
                    viewport2DVisual3D.InternalBitmapCacheBrush.Target = viewport2DVisual3D.Visual;

                    // setting the cache brush to use this new child should not invalidate our previous condition
                    Debug.Assert((viewport2DVisual3D.InternalBitmapCacheBrush.InternalTarget == null ||
                                  viewport2DVisual3D.InternalBitmapCacheBrush.InternalTarget.InheritanceContext == null),
                                 "Expected the InternalBitmapCacheBrush not to set the InheritanceContext");
                }

                if (newValue == null)
                {
                    //
                    // If we are swapping from using the BitmapCacheBrush to using the VisualBrush...
                    //

                    // Swap the brush pointing to the visual.  The cache brush will remove the dummy as the parent
                    // of the visual.
                    viewport2DVisual3D.InternalBitmapCacheBrush.Target = null;
                    viewport2DVisual3D.InternalVisualBrush.Visual      = viewport2DVisual3D.Visual;

                    // Remove the dummy child and re-add the visual as the V2DV3D's child.
                    viewport2DVisual3D.RemoveVisualChild(viewport2DVisual3D.InternalBitmapCacheBrush.InternalTarget);
                    viewport2DVisual3D.AddVisualChild(viewport2DVisual3D.Visual);

                    Debug.Assert((viewport2DVisual3D.Visual == null || viewport2DVisual3D.Visual.InheritanceContext == null),
                                 "Expected AddVisualChild to remove the InheritanceContext on Visual");
                }

                // If we changed from using one brush to the other we need to regenerate the Material.
                if (oldValue == null || newValue == null)
                {
                    viewport2DVisual3D.GenerateMaterial();
                }
            }
        }