Skip to content

Commit 2b84b51

Browse files
authored
Fix: Prevent FilePicker button double-click (#1580)
<!--- Provide a general summary of your changes in the Title above --> ## Description This PR will prevent double-clicking the button, preventing two FilePicker dialogs to appear at the same time. ## Motivation and Context Currently, if you click the buttons on FilePicker page fast enough, two FilePicker dialogs will show up. Code examples are also added to show a correct implementation. ## How Has This Been Tested? Manual. ## Screenshots (if appropriate): ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
1 parent 77635ee commit 2b84b51

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

WinUIGallery/ControlPages/FilePickerPage.xaml.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public FilePickerPage()
2727

2828
private async void PickAFileButton_Click(object sender, RoutedEventArgs e)
2929
{
30+
//disable the button to avoid double-clicking
31+
var senderButton = sender as Button;
32+
senderButton.IsEnabled = false;
33+
3034
// Clear previous returned file name, if it exists, between iterations of this scenario
3135
PickAFileOutputTextBlock.Text = "";
3236

@@ -67,10 +71,16 @@ private async void PickAFileButton_Click(object sender, RoutedEventArgs e)
6771
PickAFileOutputTextBlock.Text = "Operation cancelled.";
6872
}
6973

74+
//re-enable the button
75+
senderButton.IsEnabled = true;
7076
UIHelper.AnnounceActionForAccessibility(sender as Button, PickAFileOutputTextBlock.Text, "FilePickedNotificationId");
7177
}
7278
private async void PickAPhotoButton_Click(object sender, RoutedEventArgs e)
7379
{
80+
//disable the button to avoid double-clicking
81+
var senderButton = sender as Button;
82+
senderButton.IsEnabled = false;
83+
7484
// Clear previous returned file name, if it exists, between iterations of this scenario
7585
PickAPhotoOutputTextBlock.Text = "";
7686

@@ -114,11 +124,17 @@ private async void PickAPhotoButton_Click(object sender, RoutedEventArgs e)
114124
PickAPhotoOutputTextBlock.Text = "Operation cancelled.";
115125
}
116126

127+
//re-enable the button
128+
senderButton.IsEnabled = true;
117129
UIHelper.AnnounceActionForAccessibility(sender as Button, PickAPhotoOutputTextBlock.Text, "PhotoPickedNotificationId");
118130
}
119131

120132
private async void PickFilesButton_Click(object sender, RoutedEventArgs e)
121133
{
134+
//disable the button to avoid double-clicking
135+
var senderButton = sender as Button;
136+
senderButton.IsEnabled = false;
137+
122138
// Clear previous returned file name, if it exists, between iterations of this scenario
123139
PickFilesOutputTextBlock.Text = "";
124140

@@ -164,11 +180,17 @@ private async void PickFilesButton_Click(object sender, RoutedEventArgs e)
164180
PickFilesOutputTextBlock.Text = "Operation cancelled.";
165181
}
166182

183+
//re-enable the button
184+
senderButton.IsEnabled = true;
167185
UIHelper.AnnounceActionForAccessibility(sender as Button, PickFilesOutputTextBlock.Text, "FilesPickedNotificationId");
168186
}
169187

170188
private async void PickFolderButton_Click(object sender, RoutedEventArgs e)
171189
{
190+
//disable the button to avoid double-clicking
191+
var senderButton = sender as Button;
192+
senderButton.IsEnabled = false;
193+
172194
// Clear previous returned file name, if it exists, between iterations of this scenario
173195
PickFolderOutputTextBlock.Text = "";
174196

@@ -211,11 +233,17 @@ private async void PickFolderButton_Click(object sender, RoutedEventArgs e)
211233
PickFolderOutputTextBlock.Text = "Operation cancelled.";
212234
}
213235

236+
//re-enable the button
237+
senderButton.IsEnabled = true;
214238
UIHelper.AnnounceActionForAccessibility(sender as Button, PickFolderOutputTextBlock.Text, "FolderPickedNotificationId");
215239
}
216240

217241
private async void SaveFileButton_Click(object sender, RoutedEventArgs e)
218242
{
243+
//disable the button to avoid double-clicking
244+
var senderButton = sender as Button;
245+
senderButton.IsEnabled = false;
246+
219247
// Clear previous returned file name, if it exists, between iterations of this scenario
220248
SaveFileOutputTextBlock.Text = "";
221249

@@ -278,6 +306,8 @@ private async void SaveFileButton_Click(object sender, RoutedEventArgs e)
278306
SaveFileOutputTextBlock.Text = "Operation cancelled.";
279307
}
280308

309+
//re-enable the button
310+
senderButton.IsEnabled = true;
281311
UIHelper.AnnounceActionForAccessibility(sender as Button, SaveFileOutputTextBlock.Text, "FileSavedNotificationId");
282312
}
283313
}

WinUIGallery/ControlPagesSampleCode/System/FilePickerSample1_cs.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
private async void PickAFileButton_Click(object sender, RoutedEventArgs e)
22
{
3+
//disable the button to avoid double-clicking
4+
var senderButton = sender as Button;
5+
senderButton.IsEnabled = false;
6+
37
// Clear previous returned file name, if it exists, between iterations of this scenario
48
PickAFileOutputTextBlock.Text = "";
59

@@ -29,4 +33,7 @@
2933
{
3034
PickAFileOutputTextBlock.Text = "Operation cancelled.";
3135
}
36+
37+
//re-enable the button
38+
senderButton.IsEnabled = true;
3239
}

WinUIGallery/ControlPagesSampleCode/System/FilePickerSample2_cs.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
private async void PickAPhotoButton_Click(object sender, RoutedEventArgs e)
22
{
3+
//disable the button to avoid double-clicking
4+
var senderButton = sender as Button;
5+
senderButton.IsEnabled = false;
6+
37
// Clear previous returned file name, if it exists, between iterations of this scenario
48
PickAPhotoOutputTextBlock.Text = "";
59

@@ -32,4 +36,7 @@
3236
{
3337
PickAPhotoOutputTextBlock.Text = "Operation cancelled.";
3438
}
39+
40+
//re-enable the button
41+
senderButton.IsEnabled = true;
3542
}

WinUIGallery/ControlPagesSampleCode/System/FilePickerSample3_cs.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
private async void PickFilesButton_Click(object sender, RoutedEventArgs e)
22
{
3+
//disable the button to avoid double-clicking
4+
var senderButton = sender as Button;
5+
senderButton.IsEnabled = false;
6+
37
// Clear previous returned file name, if it exists, between iterations of this scenario
48
PickFilesOutputTextBlock.Text = "";
59

@@ -35,4 +39,7 @@
3539
{
3640
PickFilesOutputTextBlock.Text = "Operation cancelled.";
3741
}
42+
43+
//re-enable the button
44+
senderButton.IsEnabled = true;
3845
}

WinUIGallery/ControlPagesSampleCode/System/FilePickerSample4_cs.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
private async void PickFolderButton_Click(object sender, RoutedEventArgs e)
22
{
3+
//disable the button to avoid double-clicking
4+
var senderButton = sender as Button;
5+
senderButton.IsEnabled = false;
6+
37
// Clear previous returned file name, if it exists, between iterations of this scenario
48
PickFolderOutputTextBlock.Text = "";
59

@@ -30,4 +34,7 @@
3034
{
3135
PickFolderOutputTextBlock.Text = "Operation cancelled.";
3236
}
37+
38+
//re-enable the button
39+
senderButton.IsEnabled = true;
3340
}

WinUIGallery/ControlPagesSampleCode/System/FilePickerSample5_cs.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
private async void SaveFileButton_Click(object sender, RoutedEventArgs e)
22
{
3+
//disable the button to avoid double-clicking
4+
var senderButton = sender as Button;
5+
senderButton.IsEnabled = false;
6+
37
// Clear previous returned file name, if it exists, between iterations of this scenario
48
SaveFileOutputTextBlock.Text = "";
59

@@ -64,4 +68,7 @@
6468
{
6569
SaveFileOutputTextBlock.Text = "Operation cancelled.";
6670
}
71+
72+
//re-enable the button
73+
senderButton.IsEnabled = true;
6774
}

0 commit comments

Comments
 (0)