Silverlight canvas: How does it work?

In Silverlight, the Canvas class (with Reflector) has a very simple implementation: 3 attached dependency properties (Left, Top, ZIndex) and 2 ovverides of the MeasureOverride and ArrangeOverride methods, which do nothing special.

But if I use my implementation, for example:

class MyCanvas : Panel { /* Top and Left dependency properties implementation */ }

And then use MyCanvas in XAML, like a standard Canvas. This does not work as expected (I see a blank screen).

How was Canvas implemented?

- Additional code: MyCanvas.cs

public class MyCanvas : Panel
{
    public static double GetTop(DependencyObject obj)
    {
        return (double)obj.GetValue(TopProperty);
    }

    public static void SetTop(DependencyObject obj, double value)
    {
        obj.SetValue(TopProperty, value);
    }

    // Using a DependencyProperty as the backing store for Top.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TopProperty =
        DependencyProperty.RegisterAttached("Top", typeof(double), typeof(MyCanvas), new PropertyMetadata(0.0));


    public static double GetLeft(DependencyObject obj)
    {
        return (double)obj.GetValue(LeftProperty);
    }

    public static void SetLeft(DependencyObject obj, double value)
    {
        obj.SetValue(LeftProperty, value);
    }

    // Using a DependencyProperty as the backing store for Left.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LeftProperty =
        DependencyProperty.RegisterAttached("Left", typeof(double), typeof(MyCanvas), new PropertyMetadata(0.0));
}

Used in XAML, for example:

<local:MyCanvas>
    <Rectangle 
        local:MyCanvas.Left="10"
        local:MyCanvas.Top="10"
        Width="100"
        Height="100"
        Fill="Black" />
</local:MyCanvas>

if I change MyCanvas to a standard canvas, I can view the rectangle with black placeholder at position 10.10.

<Canvas>
    <Rectangle 
        Canvas.Left="10"
        Canvas.Top="10"
        Width="100"
        Height="100"
        Fill="Black" />
</Canvas>
+3
source share
3 answers

, Silverlight Reflector. Silverlight Canvas :

  • WPF Reflector Visual Studio.
  • DependencyProperties , Get Set.
  • " " "ArrangeOverride".
  • FrameworkPropertyMetadata PropertyMetadata.
  • DependencyProperty.

, Canvas ZIndex.

+2

- , , . MSDN:

, , .

, Canvas , FrameworkElement. Canrol, . , , ?

0

I can implement MeasureOverride and ArrangeOvverride something like:

    protected override Size MeasureOverride(Size availableSize)
    {
        foreach (var element in Children)
            element.Measure(availableSize);
        return base.MeasureOverride(availableSize);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        foreach (var element in Children)
        {
            var left = GetLeft(element);
            var top = GetTop(element);
            var size = element.DesiredSize;
            element.Arrange(
                new Rect(left, top, size.Width, size.Height)
                );
        }
        return base.ArrangeOverride(finalSize);
    }

And I can see the black rectangle, as expected in my MyCanvas example.

But Canvas does not implement these methods. How it works?!

0
source

Source: https://habr.com/ru/post/1725754/


All Articles