Element is already the child of another element when adding different images in a UI Element

huangapple go评论52阅读模式
英文:

Element is already the child of another element when adding different images in a UI Element

问题

我试图添加不同的页面内容以将其传递给打印管理器接口。但是,在尝试添加多个BitmapImages时,它返回一个“元素已经是另一个元素的子元素”的错误。

以下是我的当前代码示例:

Canvas page = new Canvas
{
Width = pageDescription.PageSize.Width,
Height = pageDescription.PageSize.Height
};

Canvas viewablePage = new Canvas()
{
Width = pageDescription.ViewablePageSize.Width,
Height = pageDescription.ViewablePageSize.Height
};

viewablePage.SetValue(Canvas.LeftProperty, pageDescription.Margin.Width);
viewablePage.SetValue(Canvas.TopProperty, pageDescription.Margin.Height);

// 充当视口的图像“框架”
Grid photoView = new Grid
{
Width = pageDescription.PictureViewSize.Width,
Height = pageDescription.PictureViewSize.Height
};

在foreach循环中,将不同的图像添加到不同的页面:

foreach (var btm in bitmapImages)
{
Image image = new Image
{
Source = btm,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center
};

// 当裁剪或图像小于目标区域时使用真实图像大小(以防止缩放)。
if ((btm.PixelWidth <= pageDescription.PictureViewSize.Width &&
    btm.PixelHeight <= pageDescription.PictureViewSize.Height))
{
    image.Stretch = Stretch.None;
    image.Width = bitmap.PixelWidth;
    image.Height = bitmap.PixelHeight;
}
photoView.Children.Add(image);
viewablePage.Children.Add(photoView);
page.Children.Add(viewablePage);
pages.Add(page);

}

bitmapImages是一个位图列表。如果只有一个图像要添加,它可以工作,但是当尝试将多个图像添加到页面时,会返回错误。你是否有关于如何实现这一点的建议?

非常感谢。

英文:

I am trying to add different page contents in order to pass it to the Print Manager interface. However, when trying to add multiple BitmapImages, it's returning a "Element is already the child of another element." error.

Here's what my current code looks like:

Canvas page = new Canvas
        {
            Width = pageDescription.PageSize.Width,
            Height = pageDescription.PageSize.Height
        };

        Canvas viewablePage = new Canvas()
        {
            Width = pageDescription.ViewablePageSize.Width,
            Height = pageDescription.ViewablePageSize.Height
        };

        viewablePage.SetValue(Canvas.LeftProperty, pageDescription.Margin.Width);
        viewablePage.SetValue(Canvas.TopProperty, pageDescription.Margin.Height);

        // The image &quot;frame&quot; which also acts as a viewport
        Grid photoView = new Grid
        {
            Width = pageDescription.PictureViewSize.Width,
            Height = pageDescription.PictureViewSize.Height
        };

The foreach loop wherein a different image is added to the a different page

foreach(var btm in bitmapImages)
            {
                Image image = new Image
                {
                    Source = btm,
                    HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
                    VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center
                };

                // Use the real image size when croping or if the image is smaller then the target area (prevent a scale-up).
                if ((btm.PixelWidth &lt;= pageDescription.PictureViewSize.Width &amp;&amp;
                    btm.PixelHeight &lt;= pageDescription.PictureViewSize.Height))
                {
                    image.Stretch = Stretch.None;
                    image.Width = bitmap.PixelWidth;
                    image.Height = bitmap.PixelHeight;
                }
                photoView.Children.Add(image);
                viewablePage.Children.Add(photoView);
                page.Children.Add(viewablePage);
                pages.Add(page);
            }

bitmapImages is a list of bitmaps. It works if there's only one image to be added but when adding < 1 image to a page, it returns the error. Do you have any suggestions on how should I implement this?

Thanks alot.

答案1

得分: 1

在UWP中,每个UI元素只能在UI中的一个地方同时使用。
你需要在每个循环中定义UI元素。

foreach(var btm in bitmapImages)
{
    Canvas page = new Canvas
    {
        Width = pageDescription.PageSize.Width,
        Height = pageDescription.PageSize.Height
    };

    Canvas viewablePage = new Canvas()
    {
        Width = pageDescription.ViewablePageSize.Width,
        Height = pageDescription.ViewablePageSize.Height
    };

    viewablePage.SetValue(Canvas.LeftProperty, pageDescription.Margin.Width);
    viewablePage.SetValue(Canvas.TopProperty, pageDescription.Margin.Height);

    // 作为视口的图像“框架”
    Grid photoView = new Grid
    {
        Width = pageDescription.PictureViewSize.Width,
        Height = pageDescription.PictureViewSize.Height
    };
    Image image = new Image
    {
        Source = btm,
        HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
        VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center
    };

    // 在裁剪时或图像小于目标区域时使用实际图像大小(防止放大)。
    if ((btm.PixelWidth <= pageDescription.PictureViewSize.Width &&
        btm.PixelHeight <= pageDescription.PictureViewSize.Height))
    {
        image.Stretch = Stretch.None;
        image.Width = bitmap.PixelWidth;
        image.Height = bitmap.PixelHeight;
    }
    photoView.Children.Add(image);
    viewablePage.Children.Add(photoView);
    page.Children.Add(viewablePage);
    pages.Add(page);
}
英文:

Each UI element in UWP can only be used in the UI in one place at one time.
You need to define the UI element every loop.

foreach(var btm in bitmapImages)
{
    Canvas page = new Canvas
    {
        Width = pageDescription.PageSize.Width,
        Height = pageDescription.PageSize.Height
    };

    Canvas viewablePage = new Canvas()
    {
        Width = pageDescription.ViewablePageSize.Width,
        Height = pageDescription.ViewablePageSize.Height
    };

    viewablePage.SetValue(Canvas.LeftProperty, pageDescription.Margin.Width);
    viewablePage.SetValue(Canvas.TopProperty, pageDescription.Margin.Height);

    // The image &quot;frame&quot; which also acts as a viewport
    Grid photoView = new Grid
    {
        Width = pageDescription.PictureViewSize.Width,
        Height = pageDescription.PictureViewSize.Height
    };
    Image image = new Image
    {
        Source = btm,
        HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
        VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center
    };

    // Use the real image size when croping or if the image is smaller then the target area (prevent a scale-up).
    if ((btm.PixelWidth &lt;= pageDescription.PictureViewSize.Width &amp;&amp;
        btm.PixelHeight &lt;= pageDescription.PictureViewSize.Height))
    {
        image.Stretch = Stretch.None;
        image.Width = bitmap.PixelWidth;
        image.Height = bitmap.PixelHeight;
    }
    photoView.Children.Add(image);
    viewablePage.Children.Add(photoView);
    page.Children.Add(viewablePage);
    pages.Add(page);
}

huangapple
  • 本文由 发表于 2023年2月8日 15:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定