Write this record is because in today's blog, search for information on C #, see a lot of God of God, ashamed oh, they write is to write learning steps, no technical content, but as their own learning notes, I would like to write down ... Come on...
In Hego predecessors of the works of C#--WPF, I read some today, very useful, this is his C #-WPF Web site: http://www.cnblogs.com/hegezhou_hot/category/260429.html
Everyone can go to his study, I only record my own today's study:
I used my last C # Learning II---Create a simple graphics application-WPF to do the experiment, add a button inside, renamed UI;
As shown in figure:
The code is as follows:
private void Textui_click (object sender, RoutedEventArgs e)
{
thread t = new Thread (delegate ()
{
This.userName.Text = "Test UI";
};
T.start ();
}
The following error occurred while running:
UI controls in WPF, if we look at nature, they all inherit from Dispatcherobject, so they must be scheduled and used by the UI thread, and if we manipulate interface-related elements in other background threads, the following exception message appears: (This is copied)
We change the procedure as follows:
private void Textui_click (object sender, RoutedEventArgs e)
{
thread t = new Thread (delegate ()
{
// This.userName.Text = "Test UI";
This.userName.Dispatcher.Invoke (New Action (delegate () {Username.text = "Test UI";}));
T.start ();
}
Remember to add a sentence at the beginning:
The following is the results of the operation, we first click on the UI, and then click OK, the result is as shown:
We need to know that the UI thread will eventually execute, and that we need special attention when we do multithreaded programming in WPF. In the process of MVVM programming, we need to pay attention to this problem when we modify the binding source object in our team ViewModel. (This is copied over)
Thanks to Hego, this concludes today.
OVER!!!