Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight控件具有测量/所需大小问题,图像从中间延伸到用户控件_C#_Silverlight_Xaml_Layout_Custom Controls - Fatal编程技术网

C# Silverlight控件具有测量/所需大小问题,图像从中间延伸到用户控件

C# Silverlight控件具有测量/所需大小问题,图像从中间延伸到用户控件,c#,silverlight,xaml,layout,custom-controls,C#,Silverlight,Xaml,Layout,Custom Controls,我有一个自定义控件,我正在处理它,并且它自己可以很好地工作,但是当它是设置宽度的网格时,我会遇到错误,例如: 元素“Microsoft.Windows.Design.Platform.SilverlightViewProducer+SilverlightContentHost”的布局度量重写不应将正Infinity作为其所需大小返回,即使将无穷大作为可用大小传入 这些错误只发生在我的应用程序中,而不是在我的测试工具中-但我不能在这里发布前者,但这里可能有我遗漏的东西-可能可以弥补这个问题-但如果

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

元素“Microsoft.Windows.Design.Platform.SilverlightViewProducer+SilverlightContentHost”的布局度量重写不应将正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

}
public类图像:控件
{
#区域私有常数
私有常量字符串RightImageControl=“RightImage”;
私有常量字符串MiddleImageControl=“MiddleImage”;
私有常量字符串LeftImageControl=“LeftImage”;
#端区
#区域私人成员
私家车高度;
私有int源宽度;
私有宽度=1;
可写位图裁剪;
TransformGroup transform=新建TransformGroup();
TranslateTransform位置=新的TranslateTransform();
ScaleTransform scale=新的ScaleTransform();
//控制
私有映像映像=新映像();
私有图像控制;
私有图像控制;
私有图像控制;
#端区
#区域依赖属性
公共静态只读DependencyProperty SourceProperty=
DependencyProperty.Register(“源”),typeof(BitmapSource),
typeof(UXImage),空);
公共静态只读DependencyProperty RightCapWidthProperty=
从属属性寄存器(“RightCapWidth”,类型(int),
typeof(UXImage),空);
#端区
#区域公共财产
公共位图源
{
获取{return(BitmapSource)GetValue(SourceProperty);}
设置
{
SetValue(SourceProperty,value);
}
}
公共int RightCapWidth
{
获取{return(int)GetValue(RightCapWidthProperty);}
设置
{
SetValue(RightCapWidthProperty,value);
}
}
#端区
#区域公共方法
/// 
///关于应用模板
/// 
应用程序模板()上的公共重写无效
{
base.OnApplyTemplate();
sourceHeight=Source.PixelHeight;
sourceWidth=Source.PixelWidth;
image.Source=Source;
//右元素
rightImageControl=(图像)GetTemplateChild(rightImageControl);
裁剪=新的WriteableBitmap(RightCapWidth,sourceHeight);
position.X=-sourceWidth+RightCapWidth;
裁剪。渲染(图像、位置);
crop.Invalidate();
rightImageControl。源=作物;
rightImageControl.Width=RightCapWidth;
//中间元素
middleImageControl=(图像)GetTemplateChild(middleImageControl);
裁剪=新的可写位图(中间宽度、源高度);
position.X=-sourceWidth+RightCapWidth+middleWidth;
裁剪。渲染(图像、位置);
crop.Invalidate();
middleImageControl。源=作物;
middleImageControl.Height=源高度;
//左元素
leftImageControl=(图像)GetTemplateChild(leftImageControl);
裁剪=新的WriteableBitmap(sourceWidth-RightCapWidth-middleWidth,sourceHeight);
位置X=0;
裁剪。渲染(图像、位置);
crop.Invalidate();
leftImageControl.Source=裁剪;
leftImageControl.Height=源高度;
this.Height=sourceHeight;
this.Width=sourceWidth;
}
公众形象()
{
DefaultStyleKey=typeof(UXImage);
}
#端区
}
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-Patch的UIImage,我听说过但没有见过。
有人能帮忙吗?是否有一个替代的方式去做我正在做的事情,作为一个图像元素,如果这是一个解决方案,它可以在中间延伸。

< P>我可以对正在发生的事情给出一个基本的想法和一些关于如何进行的建议。通常,
Control.Width
Control.Height
属性是由用户提供的,而不是您。控件应将这些值作为输入,并将其用作布局过程的一部分,以影响生成的
实际宽度
实际高度
属性

现在,即使您从未打算让用户在XAML中指定
Width
Height
,问题仍然是一样的。作为控件作者,您应该在
MeasureOverride
ArrangeOverride
方法调用期间告诉布局系统您所需的大小,如下所述:

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

因此,解决方案听起来很简单:在
MeasureOverride
ArrangeOverride
中计算尺寸,而不是在应用程序模板上计算尺寸,对吗?问题是,您可能无法在正确的时间获得所需的所有信息。如果您不这样做,那么您只需返回您的最佳猜测,以获得desi