根据MSDN:
UIElement首先通过测量其核心属性来开始布局过程。

评估在FrameworkElement上定义的尺寸设置属性,例如宽度,高度和边距。

应用面板特定的逻辑,例如停靠方向或堆叠方向。

在对所有孩子进行测量之后,才安排内容。

在屏幕上绘制儿童集合。

如果将其他子级添加到集合,应用LayoutTransform或调用UpdateLayout方法,则再次调用该过程。

但是UIElement的核心属性到底是什么?

最佳答案

它所说的方法是MeasureCore,在文字UIElement类上,它是这样实现的:

protected virtual Size MeasureCore(Size availableSize)
{
    return new Size(0.0, 0.0);
}


因此您可以看到它实际上对UIElement没有任何作用。但是,在FrameworkElement上是这样实现的:

protected sealed override Size MeasureCore(Size availableSize)
{
    bool useLayoutRounding = this.UseLayoutRounding;
    if (useLayoutRounding && !base.CheckFlagsAnd(VisualFlags.UseLayoutRounding))
    {
        base.SetFlags(true, VisualFlags.UseLayoutRounding);
    }
    this.ApplyTemplate();
    if (this.BypassLayoutPolicies)
    {
        return this.MeasureOverride(availableSize);
    }
    Thickness margin = this.Margin;
    double num = margin.Left + margin.Right;
    double num2 = margin.Top + margin.Bottom;
    if (useLayoutRounding && this is ScrollContentPresenter)
    {
        num = UIElement.RoundLayoutValue(num, FrameworkElement.DpiScaleX);
        num2 = UIElement.RoundLayoutValue(num2, FrameworkElement.DpiScaleY);
    }
    Size size = new Size(Math.Max(availableSize.Width - num, 0.0), Math.Max(availableSize.Height - num2, 0.0));
    FrameworkElement.MinMax minMax = new FrameworkElement.MinMax(this);
    FrameworkElement.LayoutTransformData layoutTransformData = FrameworkElement.LayoutTransformDataField.GetValue(this);
    Transform layoutTransform = this.LayoutTransform;
    if (layoutTransform != null && !layoutTransform.IsIdentity)
    {
        if (layoutTransformData == null)
        {
            layoutTransformData = new FrameworkElement.LayoutTransformData();
            FrameworkElement.LayoutTransformDataField.SetValue(this, layoutTransformData);
        }
        layoutTransformData.CreateTransformSnapshot(layoutTransform);
        layoutTransformData.UntransformedDS = default(Size);
        if (useLayoutRounding)
        {
            layoutTransformData.TransformedUnroundedDS = default(Size);
        }
    }
    else
    {
        if (layoutTransformData != null)
        {
            layoutTransformData = null;
            FrameworkElement.LayoutTransformDataField.ClearValue(this);
        }
    }
    if (layoutTransformData != null)
    {
        size = this.FindMaximalAreaLocalSpaceRect(layoutTransformData.Transform, size);
    }
    size.Width = Math.Max(minMax.minWidth, Math.Min(size.Width, minMax.maxWidth));
    size.Height = Math.Max(minMax.minHeight, Math.Min(size.Height, minMax.maxHeight));
    if (useLayoutRounding)
    {
        size = UIElement.RoundLayoutSize(size, FrameworkElement.DpiScaleX, FrameworkElement.DpiScaleY);
    }
    Size size2 = this.MeasureOverride(size);
    size2 = new Size(Math.Max(size2.Width, minMax.minWidth), Math.Max(size2.Height, minMax.minHeight));
    Size size3 = size2;
    if (layoutTransformData != null)
    {
        layoutTransformData.UntransformedDS = size3;
        Rect rect = Rect.Transform(new Rect(0.0, 0.0, size3.Width, size3.Height), layoutTransformData.Transform.Value);
        size3.Width = rect.Width;
        size3.Height = rect.Height;
    }
    bool flag = false;
    if (size2.Width > minMax.maxWidth)
    {
        size2.Width = minMax.maxWidth;
        flag = true;
    }
    if (size2.Height > minMax.maxHeight)
    {
        size2.Height = minMax.maxHeight;
        flag = true;
    }
    if (layoutTransformData != null)
    {
        Rect rect2 = Rect.Transform(new Rect(0.0, 0.0, size2.Width, size2.Height), layoutTransformData.Transform.Value);
        size2.Width = rect2.Width;
        size2.Height = rect2.Height;
    }
    double num3 = size2.Width + num;
    double num4 = size2.Height + num2;
    if (num3 > availableSize.Width)
    {
        num3 = availableSize.Width;
        flag = true;
    }
    if (num4 > availableSize.Height)
    {
        num4 = availableSize.Height;
        flag = true;
    }
    if (layoutTransformData != null)
    {
        layoutTransformData.TransformedUnroundedDS = new Size(Math.Max(0.0, num3), Math.Max(0.0, num4));
    }
    if (useLayoutRounding)
    {
        num3 = UIElement.RoundLayoutValue(num3, FrameworkElement.DpiScaleX);
        num4 = UIElement.RoundLayoutValue(num4, FrameworkElement.DpiScaleY);
    }
    SizeBox sizeBox = FrameworkElement.UnclippedDesiredSizeField.GetValue(this);
    if (flag || num3 < 0.0 || num4 < 0.0)
    {
        if (sizeBox == null)
        {
            sizeBox = new SizeBox(size3);
            FrameworkElement.UnclippedDesiredSizeField.SetValue(this, sizeBox);
        }
        else
        {
            sizeBox.Width = size3.Width;
            sizeBox.Height = size3.Height;
        }
    }
    else
    {
        if (sizeBox != null)
        {
            FrameworkElement.UnclippedDesiredSizeField.ClearValue(this);
        }
    }
    return new Size(Math.Max(0.0, num3), Math.Max(0.0, num4));
}


我知道要粘贴很多代码,但我这样做是为了说明问题。这非常复杂-非常复杂-并且继承者可以覆盖该方法以进行引导。

在此代码示例中,您看到诸如WidthHeightMargin之类的内容,并且还有更多内容用于衡量其核心。

关于c# - WPF中UIElement的核心属性是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17030784/

10-10 01:14