.net 在UI元素中添加不同图像时,元素已经是另一个元素的子元素

ajsxfq5m  于 2023-02-10  发布在  .NET
关注(0)|答案(2)|浏览(115)

我尝试添加不同的页面内容,以便将其传递到打印管理器界面。但是,当尝试添加多个位图图像时,它返回“元素已经是另一个元素的子元素。”错误。
下面是我当前的代码:

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 "frame" which also acts as a viewport
        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
                };

                // Use the real image size when croping or if the image is smaller then the target area (prevent a scale-up).
                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是一个位图列表。如果只有一个图像要添加,它可以工作,但是当添加小于1个图像到页面时,它返回错误。您有什么建议我应该如何实现它吗?
非常感谢。

4nkexdtk

4nkexdtk1#

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);

    // The image "frame" 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 <= 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);
}
rseugnpd

rseugnpd2#

尝试将Image添加到循环内部的photoView,并在循环外部仅添加一次其他子元素:

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 <= 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);

如果您仍然遇到相同的错误,请修改您的问题,解释viewablePagepagepage是什么以及如何使用它们。

相关问题