Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gathering User Input Event Handling Create Dynamic controls

Similar presentations


Presentation on theme: "Gathering User Input Event Handling Create Dynamic controls"— Presentation transcript:

1 Gathering User Input Event Handling Create Dynamic controls
Using SaveFileDialog Class Improving Responsiveness in a WPF Apps

2 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 FareTikla() {      // Buraya mouse’un sol tıklanması durumunda yapılması gereken      // işlemler gelecek. } Mouse.MouseClicked += new MouseClickedEventHandler(FareTikla);

3  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.EventHandler. XAML <MenuItem Header="_New Member" Name="newMember" Click=> C# private void newMember_Click(object sender, RoutedEventArgs e) { }

4 Create Dynamic controls
public MainWindow() { InitializeComponent(); this.Reset(); MenuItem saveMemberMenuItem = new MenuItem(); saveMemberMenuItem.Header = "Save Member Details"; saveMemberMenuItem.Click += new RoutedEventHandler(saveMember_Click); windowContextMenu = new ContextMenu(); windowContextMenu.Items.Add(saveMemberMenuItem); windowContextMenu.Items.Add(clearFormMenuItem); } private void newMember_Click(object sender, RoutedEventArgs e) ... this.ContextMenu = windowContextMenu;

5 Using SaveFileDialog Class
The SaveFileDialog 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 saveMember_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "txt"; saveDialog.FileName = "Members"; saveDialog.InitialDirectory saveDialog.Title = "Bell Ringers"; ... }

6

7 Simulate a long-running event handler in a WPF application
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 saveMember_Click(object sender, RoutedEventArgs e) { ….. Thread.Sleep(10000); MessageBox,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.

8 Perform a long-running operation on a new thread
private void saveData(string fileName) { using (StreamWriter writer = new StreamWriter(saveDialog.FileName)) writer.WriteLine("First Name: {0}", firstName.Text); writer.WriteLine("Last Name: {0}", lastName.Text); Thread.Sleep(10000); MessageBox.Show("Member details saved", "Saved"); } private void saveMember_Click(object sender, RoutedEventArgs e) ... if (saveDialog.ShowDialog().Value) Thread workerThread = new Thread(() => this.saveData(saveDialog.FileName)); workerThread.Start();

9 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.

10 Copy data from the user-interface thread to the background thread
struct Member { public string FirstName { get; set; } public string LastName { get; set; } } private void saveData(string fileName, Member member) using (StreamWriter writer = new StreamWriter(fileName)) writer.WriteLine("First Name: {0}", member.FirstName); writer.WriteLine("Last Name: {0}", member.LastName); Thread.Sleep(10000); MessageBox.Show("Member details saved", "Saved");

11 private void saveMember_Click(object sender, RoutedEventArgs e) {
private void saveMember_Click(object sender, RoutedEventArgs e) { ... if (saveDialog.ShowDialog().Value) Member member = new Member(); member.FirstName = firstName.Text; member.LastName = lastName.Text; Thread workerThread = new Thread( () => this.saveData(saveDialog.FileName, member)); workerThread.Start(); }

12 An alternative approach is to create a BackgroundWorker object.
private void bnSync_Click( object sender, EventArgs e ) { // Create a background thread BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler( bw_DoWork ); bw.RunWorkerAsync(); } You specify the method that a BackgroundWorker object runs by subscribing to the DoWork event. The DoWork event expects you to provide a DoWorkEventHandler delegate

13


Download ppt "Gathering User Input Event Handling Create Dynamic controls"

Similar presentations


Ads by Google