如何将 UIElement 添加到 List中?

发布于 2024-10-30 01:26:13 字数 784 浏览 6 评论 0原文

我尝试将两个 Canvas 添加到 List,但我从以下代码中收到异常:

List<Canvas> cvList = new List<Canvas>();

Canvas cv = new Canvas();
cv.Width = 100; 
cv.Height = 100;

cvList.Add(cv); // adding first Canvas to List<Canvas>
cvList.Add(cv); // adding the second Canvas to List<Canvas>
...

为了详细说明该问题,每个 Canvas code> 必须是不同的,因为每个子元素可能有不同的 TextBox、Label 和其他 UIElement。所以我认为上面的代码不应该工作。但是,尽管我不能这样做:

Canvas cv1 = new Canvas();
cv1.Width = 100;
Canvas cv2 = new Canvas();
cv2.Width = 250;
...

Or 

Canvas[] cv = new Canvas[myInt];

我不能执行上述操作,因为列表的大小是在运行时确定的,并且我无法为数组分配大小或单独声明每个数组。

如何正确地做到这一点?是的,我已经阅读了 MSDN 上的列表,但该网站没有告诉我如何操作。谢谢。

I try to add two Canvas to a List<Canvas>, but I receive exception from the following code:

List<Canvas> cvList = new List<Canvas>();

Canvas cv = new Canvas();
cv.Width = 100; 
cv.Height = 100;

cvList.Add(cv); // adding first Canvas to List<Canvas>
cvList.Add(cv); // adding the second Canvas to List<Canvas>
...

To elaborate more on the issue, each Canvas has to be distinct since each may Children different TextBox, Label and other UIElement. So I think the above code shouldn't work. However though I cannot do this:

Canvas cv1 = new Canvas();
cv1.Width = 100;
Canvas cv2 = new Canvas();
cv2.Width = 250;
...

Or 

Canvas[] cv = new Canvas[myInt];

I cannot do the above because the size of the List is determine at run time and I cannot assign a size to an Array or declare each array individually.

How to do this correctly? Yes, I've read the List on MSDN, but the site didn't tell me how to do so. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-11-06 01:26:13

要详细说明 Joels 的答案,这就是您需要做的:

List<Canvas> cvList = new List<Canvas>();

Canvas canvas1 = new Canvas();
canvas1.Width = 100; 
canvas1.Height = 100;
cvList.Add(canvas1);

Canvas canvas2 = new Canvas();
canvas2.Width = 100; 
canvas2.Height = 100;
cvList.Add(canvas2);

请注意,以这种方式将相同的元素两次添加到同一个 List 集合中是完全合法的,但是尝试使用相同的元素布局中两次(根据此列表的使用方式可能会发生)不是。

To elaborate on Joels answer, this is what you need to do:

List<Canvas> cvList = new List<Canvas>();

Canvas canvas1 = new Canvas();
canvas1.Width = 100; 
canvas1.Height = 100;
cvList.Add(canvas1);

Canvas canvas2 = new Canvas();
canvas2.Width = 100; 
canvas2.Height = 100;
cvList.Add(canvas2);

Note that adding the same element twice to the same List<Canvas> collection in this way is perfectly legal, however attempting to use the same element twice in a layout (as might happen depending on the way that this list is used) is not.

辞慾 2024-11-06 01:26:13

您正在将相同的画布添加到列表中。如果您想要列表中有两个不同的画布,则必须制作两个画布。请注意,您可以使用相同的变量来执行此操作,只需确保在将它们添加到列表之间再次使用 new 运算符即可。

You're adding the same canvas to the list. If you want two different canvases in the list, you have to make two canvases. Note that you can do this with the same variable, just make sure you use the new operator again in between adding them to list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文