Silverlight控件存在度量值/所需大小问题,图像从中间用户控件拉伸



我有一个正在处理的自定义控件,它自己也能很好地工作,但当它是设置宽度的网格时,我会收到错误,例如:

元素"Microsoft"的布局度量重写。Windows。设计站台SilverlightViewProducer+SilverlightContentHost"不应将PositiveInfinity作为其DesiredSize返回,即使Infinity是作为可用大小传入的。

这些错误只发生在我的应用程序中,而不是在我的测试工具中-但我不能在这里发布前者,但这里可能有我遗漏的东西-可能可以弥补这个问题-但如果有人能帮忙,那将是很棒的

这是分类代码:

public class UXImage : Control
    {
        #region Private Constants
        private const string RightImageControl = "RightImage";
        private const string MiddleImageControl = "MiddleImage";
        private const string LeftImageControl = "LeftImage";
        #endregion
    #region Private Members
    private int sourceHeight;
    private int sourceWidth;
    private int middleWidth = 1;
    WriteableBitmap crop;
    TransformGroup transform = new TransformGroup();
    TranslateTransform position = new TranslateTransform();
    ScaleTransform scale = new ScaleTransform();
    // Controls
    private Image image = new Image();
    private Image rightImageControl;
    private Image middleImageControl;
    private Image leftImageControl;
    #endregion
    #region Dependency Properties
    public static readonly DependencyProperty SourceProperty =
    DependencyProperty.Register("Source", typeof(BitmapSource),
    typeof(UXImage), null);
    public static readonly DependencyProperty RightCapWidthProperty =
    DependencyProperty.Register("RightCapWidth", typeof(int),
    typeof(UXImage), null);
    #endregion
    #region Public Properties
    public BitmapSource Source
    {
        get { return (BitmapSource)GetValue(SourceProperty); }
        set
        {
            SetValue(SourceProperty, value);
        }
    }
    public int RightCapWidth
    {
        get { return (int)GetValue(RightCapWidthProperty); }
        set
        {
            SetValue(RightCapWidthProperty, value);
        }
    }
    #endregion
    #region Public Methods
    /// <summary>
    /// On Apply Template
    /// </summary>
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        sourceHeight = Source.PixelHeight;
        sourceWidth = Source.PixelWidth;
        image.Source = Source;
        // Right Element
        rightImageControl = (Image)GetTemplateChild(RightImageControl);
        crop = new WriteableBitmap(RightCapWidth, sourceHeight);
        position.X = -sourceWidth + RightCapWidth;
        crop.Render(image, position);
        crop.Invalidate();
        rightImageControl.Source = crop;
        rightImageControl.Width = RightCapWidth;
        // Middle Element
        middleImageControl = (Image)GetTemplateChild(MiddleImageControl);
        crop = new WriteableBitmap(middleWidth, sourceHeight);
        position.X = -sourceWidth + RightCapWidth + middleWidth;
        crop.Render(image, position);
        crop.Invalidate();
        middleImageControl.Source = crop;
        middleImageControl.Height = sourceHeight;
        // Left Element
        leftImageControl = (Image)GetTemplateChild(LeftImageControl);
        crop = new WriteableBitmap(sourceWidth - RightCapWidth - middleWidth, sourceHeight);
        position.X = 0;
        crop.Render(image, position);
        crop.Invalidate();
        leftImageControl.Source = crop;
        leftImageControl.Height = sourceHeight;
        this.Height = sourceHeight;
        this.Width = sourceWidth;
    }
    public UXImage()
    {
        DefaultStyleKey = typeof(UXImage);
    }
    #endregion
}

Generic.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:local="clr-namespace:UXLibrary">
<Style TargetType="local:UXImage">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:UXImage">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Image x:Name="LeftImage" Stretch="Uniform" Source="{TemplateBinding Source}" Grid.Column="0"/>
                        <Image x:Name="MiddleImage" Stretch="Fill" Grid.Column="1"/>
                        <Image x:Name="RightImage" Stretch="Uniform" Grid.Column="2"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</ResourceDictionary>

用法:

<ux:UXImage RightCapWidth="10" Source="Images/example.png"/>

我在Silverlight中还没有见过这样的控件,它基于iOS和Android 9补丁中的UIImage,我听说过但没有见过。有人能帮忙吗?如果这是一个解决方案,那么有没有一种替代方法可以将我正在做的事情作为一个可以在中间拉伸的图像元素来做。

我可以对正在发生的事情给出一个基本的想法,并对如何进行提出一些建议。通常,Control.WidthControl.Height属性由用户提供,而不是由您提供。控件应该将这些值作为输入,并将它们用作布局过程的一部分,以影响生成的ActualWidthActualHeight属性。

现在,即使您从未打算让用户在XAML中指定WidthHeight,问题仍然存在。作为控件的作者,您应该在MeasureOverrideArrangeOverride方法调用期间告诉布局系统您想要的大小,如下所述:

  • 布局系统

特别是,由于您尚未覆盖MeasureOverride,因此默认行为是返回第一个视觉子级的所需大小,在您的情况下,它将是Grid,其大小未指定,因此希望尽可能大,即无限大。

所以,解决方案听起来很简单:用MeasureOverrideArrangeOverride计算尺寸,而不是用OnApplyTemplate,对吧?问题是,您可能没有在正确的时间获得所需的所有信息。如果没有,则只需返回您对所需大小的最佳猜测,然后当您获得正确的信息时,强制执行新的布局过程,以便再次调用MeasureOverride。上面的链接和文档通常描述了如何做到这一点。

最新更新