英文:
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 "frame" 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 <= 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 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 "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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论