Gathering User Input Event Handling Create Dynamic controls

  • Slides: 13
Download presentation
Gathering User Input • • Event Handling Create Dynamic controls Using Save. File. Dialog

Gathering User Input • • Event Handling Create Dynamic controls Using Save. File. Dialog Class Improving Responsiveness in a WPF Apps

Event Handling �When a user interacts with a GUI control (e. g. , clicking

Event Handling �When a user interacts with a GUI control (e. g. , clicking a button on a form), one or more methods are executed in response to the above event. � Events can also be generated without user interactions. Event handlers are methods in an object that are executed in response to some events occurring in the application private void Fare. Tikla() { // Buraya mouse’un sol tıklanması durumunda yapılması gereken // işlemler gelecek. } � Mouse. Clicked += new Mouse. Clicked. Event. Handler(Fare. Tikla);

� You will observe that you do not have to declare the delegates and

� You will observe that you do not have to declare the delegates and reference those delegates using event keyword because the events (mouse click, etc. ) for the GUI controls (Form, Button, etc. ) are already available to you and the delegate is System. Event. Handler. XAML <Menu. Item Header="_New Member" Name="new. Member" Click=> C# private void new. Member_Click(object sender, Routed. Event. Args e) { }

Create Dynamic controls public Main. Window() { Initialize. Component(); this. Reset(); Menu. Item save.

Create Dynamic controls public Main. Window() { Initialize. Component(); this. Reset(); Menu. Item save. Member. Menu. Item = new Menu. Item(); save. Member. Menu. Item. Header = "Save Member Details"; save. Member. Menu. Item. Click += new Routed. Event. Handler(save. Member_Click); window. Context. Menu = new Context. Menu(); window. Context. Menu. Items. Add(save. Member. Menu. Item); window. Context. Menu. Items. Add(clear. Form. Menu. Item); } private void new. Member_Click(object sender, Routed. Event. Args e) {. . . this. Context. Menu = window. Context. Menu; }

Using Save. File. Dialog Class � The Save. File. Dialog component allows users to

Using Save. File. Dialog Class � The Save. File. Dialog component allows users to browse the file system and select files to be saved. The dialog box returns the path and name of the file the user has selected in the dialog box. However, you must write the code to actually write the files to disk. private void save. Member_Click(object sender, Routed. Event. Args e) { Save. File. Dialog save. Dialog = new Save. File. Dialog(); save. Dialog. Default. Ext = "txt"; save. Dialog. File. Name = "Members"; save. Dialog. Initial. Directory = @"C: UsersYour. NameDocuments"; save. Dialog. Title = "Bell Ringers"; . . . }

Improving Responsiveness in a WPF Apps � You have seen that your application responds

Improving Responsiveness in a WPF Apps � You have seen that your application responds to the user performing operations such as clicking buttons, typing text into boxes, or selecting menu items by using code that runs when the corresponding events are triggered. However, what happens if the code that responds to an event takes a long time to run? Simulate a long-running event handler in a WPF application private void save. Member_Click(object sender, Routed. Event. Args e) { …. . Thread. Sleep(10000); Message. Box, Show("Member details saved", "Saved"); } � The static Sleep method of the Thread class in the System. Threading namespace causes the current thread in the application to stop responding for the specified period of time.

 Perform a long-running operation on a new thread private void save. Data(string file.

Perform a long-running operation on a new thread private void save. Data(string file. Name) { using (Stream. Writer writer = new Stream. Writer(save. Dialog. File. Name)) { writer. Write. Line("First Name: {0}", first. Name. Text); writer. Write. Line("Last Name: {0}", last. Name. Text); Thread. Sleep(10000); Message. Box. Show("Member details saved", "Saved"); } } private void save. Member_Click(object sender, Routed. Event. Args e) {. . . if (save. Dialog. Show. Dialog(). Value) { Thread worker. Thread = new Thread(() => this. save. Data(save. Dialog. File. Name)); worker. Thread. Start(); } }

 • The problem is that the security model implemented by WPF prevents any

• The problem is that the security model implemented by WPF prevents any threads other than the thread that created a user interface object such as a control from accessing that object. This restriction prevents two or more threads from attempting to take control of the user input or modifying the data on the screen because this could result in corruption of your data.

 Copy data from the user-interface thread to the background thread struct Member {

Copy data from the user-interface thread to the background thread struct Member { public string First. Name { get; set; } public string Last. Name { get; set; } } private void save. Data(string file. Name, Member member) { using (Stream. Writer writer = new Stream. Writer(file. Name)) { writer. Write. Line("First Name: {0}", member. First. Name); writer. Write. Line("Last Name: {0}", member. Last. Name); Thread. Sleep(10000); Message. Box. Show("Member details saved", "Saved"); } }

private void save. Member_Click(object sender, Routed. Event. Args e) {. . . if (save.

private void save. Member_Click(object sender, Routed. Event. Args e) {. . . if (save. Dialog. Show. Dialog(). Value) { Member member = new Member(); member. First. Name = first. Name. Text; member. Last. Name = last. Name. Text; Thread worker. Thread = new Thread( () => this. save. Data(save. Dialog. File. Name, member)); worker. Thread. Start(); } }

An alternative approach is to create a Background. Worker object. private void bn. Sync_Click(

An alternative approach is to create a Background. Worker object. private void bn. Sync_Click( object sender, Event. Args e ) { // Create a background thread Background. Worker bw = new Background. Worker(); bw. Do. Work += new Do. Work. Event. Handler( bw_Do. Work ); bw. Run. Worker. Async(); } � You specify the method that a Background. Worker object runs by subscribing to the Do. Work event. The Do. Work event expects you to provide a Do. Work. Event. Handler delegate