C#问题:如何删除Grid对象 [英] C# question: How do you delete a Grid object

查看:114
本文介绍了C#问题:如何删除Grid对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序(WPF),我在

应用程序中有一个Panel,它将动态填充

类型Grid,TextBox的对象为了简单起见,让我们说我只是添加和删除网格对象的
。我可以通过创建Grid

对象轻松添加对象。我遇到的问题是我不知道如何删除

对象。你如何删除Grid对象?我宁愿不必使用

将可见转为可见。关闭并等待垃圾收集器

到来。


有什么想法吗?

I''m building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let''s say I''m just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don''t know how to delete the
objects. How do you delete a Grid object? I''d prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

Any ideas?

推荐答案

转动可见开关不会导致GC收集对象。

它只会将控件的可见性设置为false,作为对
对象仍然由父对象保存。


我也不知道你是如何使用Panel而不是派生类的

面板标记为抽象。


无论如何,任何来自Panel的类都有一个

Children属性拥有面板上的所有子控件。

将你的Grid传递给UIElementCollection上的Remove方法,即通过Children属性公开的
,它将被删除。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.c om


< do ******** @ hotmail.comwrote in message

news:11 ***** *****************@19g2000hsx.googlegro ups.com ...
Turning the visible switch will not cause the GC to collect the object.
It will just set the visibility of the control to false, as the reference to
the object will still be held by the parent object.

I also don''t know how you are using a Panel, and not a derived class, as
Panel is marked abstract.

Regardless, any class that derives from Panel that you are using has a
Children property which has all the child controls that are on the panel.
Pass your Grid to the Remove method on the UIElementCollection that is
exposed through the Children property and it will be removed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...

我正在构建一个应用程序(WPF)我在

应用程序中有一个Panel,它将动态填充

类型Grid,TextBox等对象。为简单起见,让's'说我只是添加和

删除网格对象。我可以通过创建Grid

对象轻松添加对象。我遇到的问题是我不知道如何删除

对象。你如何删除Grid对象?我宁愿不必使用

将可见转为可见。关闭并等待垃圾收集器

到来。


任何想法?
I''m building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let''s say I''m just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don''t know how to delete the
objects. How do you delete a Grid object? I''d prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

Any ideas?



< do ******** @ hotmail.comwrote in message

news:11 ********** ************@19g2000hsx.googlegro ups.com ...

<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...

我正在构建一个应用程序(WPF)和我在

应用程序中有一个Panel,它将动态填充

类型Grid,TextBox等对象。为简单起见,让我们说我是只需添加和

删除网格对象。我可以通过创建Grid

对象轻松添加对象。我遇到的问题是我不知道如何删除

对象。
I''m building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let''s say I''m just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don''t know how to delete the
objects.


如何删除Grid对象?我宁愿不必使用

将可见转为可见。关闭并等待垃圾收集器

到来。
How do you delete a Grid object? I''d prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.



你总是要等垃圾收集器出现;你不能

在.NET中明确释放内存..在任何情况下,使一个对象不是可见的
不会将它标记为垃圾收集,它会使它不可见


不能说我曾经做过你正在做的事情,但你不能只是网格。请勿()

it?

You always have to wait for the garbage collector to come around; you can''t
deallocate memory explicitly in .NET .. in any case, making an object not
visible doesn''t mark it for garbage collection, it makes it not visible

Can''t say I''ve ever done what you''re doing but can''t you just grid.Dispose()
it?


Liz写道:
Liz wrote:

< do ***** ***@hotmail.com写信息

新闻:11 ********************** @ 19g2000hsx.googlegro ups.com。 ..
<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...

>我正在构建一个应用程序(WPF),我在
应用程序中有一个Panel,它将动态填充对象
类型Grid,TextBox等。为简单起见,让我们说我只是添加和删除Grid对象。我可以通过创建Grid
对象轻松添加对象。我遇到的问题是我不知道如何删除
对象。
>I''m building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let''s say I''m just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don''t know how to delete the
objects.


>如何删除Grid对象?我宁愿不必翻开可见字样。关闭并等待垃圾收集器到处走动。
>How do you delete a Grid object? I''d prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.



你总是要等垃圾收集器出现;


You always have to wait for the garbage collector to come around;



这并不完全正确。您可以使用stackalloc在堆栈(远离垃圾收集器)上分配内存。


如果您经常需要适度分配,那就太好了

对象的大数组。在这种情况下,垃圾收集会受到伤害。


Alun Harford

That''s not entirely true. You can use stackalloc to allocate memory on
the stack (away from the garbage collector).

It''s good if you frequently need to allocate moderately large arrays of
objects. Garbage collection hurts in that situation.

Alun Harford


这篇关于C#问题:如何删除Grid对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆