Exemplo n.º 1
0
		public void LayoutUpdatedAndLoaded ()
		{
			List<string> events = new List<string>();

			EventHandler handler = delegate { events.Add ("LayoutUpdated"); };

			Rectangle r = new Rectangle ();

			r.LayoutUpdated += handler;
			r.Loaded += delegate { events.Add ("Loaded"); };

			TestPanel.Children.Add (r);

			r.UpdateLayout ();
			Assert.AreEqual (1, events.Count, "#1");
			Assert.AreEqual ("LayoutUpdated", events [0], "#2");
			Enqueue (() => {
				Assert.IsTrue (events.Count >= 2, "#3");
				Assert.AreEqual ("Loaded", events [1], "#4");
				r.LayoutUpdated -= handler;
			});
			EnqueueTestComplete ();
		}
    protected override Rectangle InitializeAdornedElementImage()
    {
      Rect adornedBounds = VisualTreeHelper.GetDescendantBounds( this.AdornedElement );

      // Round the height and width of the bounds to reduce the 
      // blur effect caused by the RenderTargetBitmap. 
      // When drawing Text into RenderTargetBitmap, the ClearType 
      // reverts to grayscale causing a blur effect. If there is 
      // also an extrapolation, the blur effect will be worst.
      int roundedHeight = ( int )Math.Round( adornedBounds.Height, MidpointRounding.ToEven );
      int roundedWidth = ( int )Math.Round( adornedBounds.Width, MidpointRounding.ToEven );

      VisualBrush brush = new VisualBrush( this.AdornedElement );

      Rectangle rectangle = new Rectangle();

      // Only if we have something to adorn
      if( this.DeepCopy
        && ( ( roundedWidth > 0 ) && ( roundedHeight > 0 ) ) )
      {
        try
        {
          RenderTargetBitmap bitmap = new RenderTargetBitmap( roundedWidth,
            roundedHeight,
            96,
            96,
            PixelFormats.Pbgra32 );

          DrawingVisual drawingVisual = new DrawingVisual();

          using( DrawingContext context = drawingVisual.RenderOpen() )
          {
            Rect finalRect = new Rect( 0,
                0,
                roundedWidth,
                roundedHeight );

            context.DrawRectangle( brush, null, finalRect );
          }

          bitmap.Render( drawingVisual );

          // Ensure to set the Height and Width
          // values for the Fill does not resize the 
          // rectangle if it is larger. This also
          // reduce the blur effect.
          rectangle.Height = roundedHeight;
          rectangle.Width = roundedWidth;
          rectangle.UpdateLayout();

          // Adding BitmapScallingMode using any other BitmapScalingMode cause some
          // blur in the resulting Bitmap
          RenderOptions.SetBitmapScalingMode( rectangle, BitmapScalingMode.NearestNeighbor );
          rectangle.Fill = new ImageBrush( bitmap );

          // Translate the Top Left corner of the rectangle that
          // contains the AdornedElement
          if( !adornedBounds.Size.IsEmpty )
          {
            rectangle.RenderTransform = new TranslateTransform( adornedBounds.X, adornedBounds.Y );
          }
        }
        catch( Exception )
        {
          // Avoid any exception and use the brush itself
          rectangle.Fill = brush;
        }
      }
      else
      {
        rectangle.Fill = brush;
      }

      return rectangle;
    }