如何从静态方法更新控件?
你好 为什么我无法从静态方法访问表单上的私有控件(例如 ListBox)? 在这种情况下如何更新控制?
编辑1.
我的代码:
ThreadStart thrSt = new ThreadStart(GetConnected);
Thread thr = new Thread(thrSt);
thr.Start();
所以
static void GetConnected()
{
//update my ListBox
}
它必须是无效的,没有参数并且是静态的,对吧?
编辑2。
如果有人需要 WPF 中的解决方案,那么应该尝试以下操作:
private void GetConnected()
{
myListBox.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(() =>
{
myListBox.Items.Add("something");
}
)
);
}
Hello
Why I haven't access to my private control on form (e.g. ListBox) from a static method?
How to update control in this case?
EDIT 1.
my code:
ThreadStart thrSt = new ThreadStart(GetConnected);
Thread thr = new Thread(thrSt);
thr.Start();
and
static void GetConnected()
{
//update my ListBox
}
So it must be void, without param and be static, right?
EDIT 2.
If someone need solution in WPF then should try this:
private void GetConnected()
{
myListBox.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(() =>
{
myListBox.Items.Add("something");
}
)
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在网上找到了另一个答案,
将其写在表单类中:
和表单构造函数中:
现在我们可以使用变量“frm”来访问表单上的所有控件。
静态方法中的某处:
i found another answer on the web
write this in the form class:
and in the form constructor:
now we can use the variable "frm" for accessing all of controls on the form.
somewhere in a static method:
静态方法无法访问实例状态(例如非静态控件)。从方法声明中删除
static
,或者将控件的引用作为参数传递给方法:...并像这样调用它:
Update
有多种方法可以在 UI 中执行异步操作(现在假设为 winforms)。我建议您考虑使用
BackgroundWorker
(在此处搜索 SO;有很多示例)。如果您确实想通过自己创建线程来做到这一点,那么这是一种方法:...并调用它:
您还可以使用线程池(优于创建自己的线程,因为您不这样做)不要期望它们运行很长时间):
Static methods cannot access instance state (such as a non-static control). Either remove
static
from the method declaration, or pass a reference to the control as argument to the method:...and call it like so:
Update
There are different ways to do asynchronous things in the UI (now assuming winforms). I would recommend you to look into using
BackgroundWorker
(search here on SO; plenty of examples). If you really want to do it by creating threads on your own, here is one way to do that:...and to call it:
You can also use the thread pool (preferred over creating your own threads, given that you don't expect them to run for very long):
我刚刚找到了一种新的、不同的方法来从静态方法更新控件。但我们必须为我们的控件选择唯一的名称
I just found a new and different way to update a control from a static method. but we have to choose unique names for our controls
您需要将对控件的引用传递给静态方法或包含它们的东西。静态方法无法访问非静态字段/方法/等。不要将您的控件声明为静态,我什至不确定它是否可能,但如果是,它会导致您甚至不想知道的问题。
you need to pass a reference to the control to your static method, or something that contains them. static methods cant access non static fields/methods/etc. dont declare your control as static, i'm not even sure if its possible, but if it was, it would cause you problems you dont even want to know about.
从静态方法将项目添加到列表框的最简单解决方案:
public static ListBox frm;
frm = this.listBox1;
Form1.frm.Items.Add(myitem)
easiest solution for adding an item to a listbox from a static method:
public static ListBox frm;
frm = this.listBox1;
Form1.frm.Items.Add(myitem)
您无法从静态函数访问“this”、“ui”或 MainWindow 中的任何函数。
创建一个Mainwindow的公共指针
为了解决这个问题,在调用回调函数(静态函数)之前先
,将this赋给指针THIS ,现在可以使用THIS来代替this。
例如:
You cannot access "this", "ui" or any function in MainWindow from a static function.
To solve this problem, make a public pointer of Mainwindow
before calling the callback function (the static function), assign this to the pointer THIS
Now, you can use THIS instead of this.
for example: