c# - How can I set the visibility of an element from another class in WPF? -


i have little program class. in main thread start thread starts method in class. set visibility of label class. have tried so:

mainwindow main = new mainwindow(); ... main.lable.visibility = visibility.visible; 

the program gives me error thread must sta-thread.

how can that? thank you!

the thread modifies ui must sta thread. must have message loop, talking application main thread. can read more wpf threading here.

the point have use dispatcher.begininvoke method.

to solve problem, supposing lable public, in secondary thread try use code:

main.dispatcher.begininvoke(     dispatcherpriority.normal,      new action(() => main.lable.visibility = visibility.visible)); 

i hope can you.

edit

i edit answer simple example updating ui secondary thread:

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();          task task = new task(new action(() => executeonseparatethread()));         task.start();     }      private void executeonseparatethread()     {         thread.sleep(2000);          this.dispatcher.begininvoke(dispatcherpriority.normal,              new action(() => label.foreground = brushes.red));     } } 

i not know complete code, can providing example can lead on right path.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -