|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Windows; |
| 6 | +using System.Windows.Controls; |
| 7 | +using System.Windows.Data; |
| 8 | +using System.Windows.Documents; |
| 9 | +using System.Windows.Input; |
| 10 | +using System.Windows.Media; |
| 11 | +using System.Windows.Media.Imaging; |
| 12 | +using System.Windows.Navigation; |
| 13 | +using System.Windows.Shapes; |
| 14 | +using System.Threading; |
| 15 | +using System.ComponentModel; |
| 16 | + |
| 17 | +namespace Ookii.Dialogs.Wpf.Sample |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Interaction logic for Window1.xaml |
| 21 | + /// </summary> |
| 22 | + public partial class MainWindow : Window |
| 23 | + { |
| 24 | + private ProgressDialog _sampleProgressDialog = new ProgressDialog() |
| 25 | + { |
| 26 | + WindowTitle = "Progress dialog sample", |
| 27 | + Text = "This is a sample progress dialog...", |
| 28 | + Description = "Processing...", |
| 29 | + ShowTimeRemaining = true, |
| 30 | + }; |
| 31 | + |
| 32 | + public MainWindow() |
| 33 | + { |
| 34 | + InitializeComponent(); |
| 35 | + |
| 36 | + _sampleProgressDialog.DoWork += new System.ComponentModel.DoWorkEventHandler(_sampleProgressDialog_DoWork); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + private void _showDialogButton_Click(object sender, RoutedEventArgs e) |
| 41 | + { |
| 42 | + switch( _dialogComboBox.SelectedIndex ) |
| 43 | + { |
| 44 | + case 0: |
| 45 | + ShowTaskDialog(); |
| 46 | + break; |
| 47 | + case 1: |
| 48 | + ShowTaskDialogWithCommandLinks(); |
| 49 | + break; |
| 50 | + case 2: |
| 51 | + ShowProgressDialog(); |
| 52 | + break; |
| 53 | + case 3: |
| 54 | + ShowCredentialDialog(); |
| 55 | + break; |
| 56 | + case 4: |
| 57 | + ShowFolderBrowserDialog(); |
| 58 | + break; |
| 59 | + case 5: |
| 60 | + ShowOpenFileDialog(); |
| 61 | + break; |
| 62 | + case 6: |
| 63 | + ShowSaveFileDialog(); |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void ShowTaskDialog() |
| 69 | + { |
| 70 | + if( TaskDialog.OSSupportsTaskDialogs ) |
| 71 | + { |
| 72 | + using( TaskDialog dialog = new TaskDialog() ) |
| 73 | + { |
| 74 | + dialog.WindowTitle = "Task dialog sample"; |
| 75 | + dialog.MainInstruction = "This is an example task dialog."; |
| 76 | + dialog.Content = "Task dialogs are a more flexible type of message box. Among other things, task dialogs support custom buttons, command links, scroll bars, expandable sections, radio buttons, a check box (useful for e.g. \"don't show this again\"), custom icons, and a footer. Some of those things are demonstrated here."; |
| 77 | + dialog.ExpandedInformation = "Ookii.org's Task Dialog doesn't just provide a wrapper for the native Task Dialog API; it is designed to provide a programming interface that is natural to .Net developers."; |
| 78 | + dialog.Footer = "Task Dialogs support footers and can even include <a href=\"http://www.ookii.org\">hyperlinks</a>."; |
| 79 | + dialog.FooterIcon = TaskDialogIcon.Information; |
| 80 | + dialog.EnableHyperlinks = true; |
| 81 | + TaskDialogButton customButton = new TaskDialogButton("A custom button"); |
| 82 | + TaskDialogButton okButton = new TaskDialogButton(ButtonType.Ok); |
| 83 | + TaskDialogButton cancelButton = new TaskDialogButton(ButtonType.Cancel); |
| 84 | + dialog.Buttons.Add(customButton); |
| 85 | + dialog.Buttons.Add(okButton); |
| 86 | + dialog.Buttons.Add(cancelButton); |
| 87 | + dialog.HyperlinkClicked += new EventHandler<HyperlinkClickedEventArgs>(TaskDialog_HyperLinkClicked); |
| 88 | + TaskDialogButton button = dialog.ShowDialog(this); |
| 89 | + if( button == customButton ) |
| 90 | + MessageBox.Show(this, "You clicked the custom button", "Task Dialog Sample"); |
| 91 | + else if( button == okButton ) |
| 92 | + MessageBox.Show(this, "You clicked the OK button.", "Task Dialog Sample"); |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + MessageBox.Show(this, "This operating system does not support task dialogs.", "Task Dialog Sample"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void ShowTaskDialogWithCommandLinks() |
| 102 | + { |
| 103 | + if( TaskDialog.OSSupportsTaskDialogs ) |
| 104 | + { |
| 105 | + using( TaskDialog dialog = new TaskDialog() ) |
| 106 | + { |
| 107 | + dialog.WindowTitle = "Task dialog sample"; |
| 108 | + dialog.MainInstruction = "This is a sample task dialog with command links."; |
| 109 | + dialog.Content = "Besides regular buttons, task dialogs also support command links. Only custom buttons are shown as command links; standard buttons remain regular buttons."; |
| 110 | + dialog.ButtonStyle = TaskDialogButtonStyle.CommandLinks; |
| 111 | + TaskDialogButton elevatedButton = new TaskDialogButton("An action requiring elevation"); |
| 112 | + elevatedButton.CommandLinkNote = "Both regular buttons and command links can show the shield icon to indicate that the action they perform requires elevation. It is up to the application to actually perform the elevation."; |
| 113 | + elevatedButton.ElevationRequired = true; |
| 114 | + TaskDialogButton otherButton = new TaskDialogButton("Some other action"); |
| 115 | + TaskDialogButton cancelButton = new TaskDialogButton(ButtonType.Cancel); |
| 116 | + dialog.Buttons.Add(elevatedButton); |
| 117 | + dialog.Buttons.Add(otherButton); |
| 118 | + dialog.Buttons.Add(cancelButton); |
| 119 | + dialog.ShowDialog(this); |
| 120 | + } |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + MessageBox.Show(this, "This operating system does not support task dialogs.", "Task Dialog Sample"); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void ShowProgressDialog() |
| 129 | + { |
| 130 | + if( _sampleProgressDialog.IsBusy ) |
| 131 | + MessageBox.Show(this, "The progress dialog is already displayed.", "Progress dialog sample"); |
| 132 | + else |
| 133 | + _sampleProgressDialog.Show(); // Show a modeless dialog; this is the recommended mode of operation for a progress dialog. |
| 134 | + } |
| 135 | + |
| 136 | + private void ShowCredentialDialog() |
| 137 | + { |
| 138 | + using( CredentialDialog dialog = new CredentialDialog() ) |
| 139 | + { |
| 140 | + // The window title will not be used on Vista and later; there the title will always be "Windows Security". |
| 141 | + dialog.WindowTitle = "Credential dialog sample"; |
| 142 | + dialog.MainInstruction = "Please enter your username and password."; |
| 143 | + dialog.Content = "Since this is a sample the credentials won't be used for anything, so you can enter anything you like."; |
| 144 | + dialog.ShowSaveCheckBox = true; |
| 145 | + dialog.ShowUIForSavedCredentials = true; |
| 146 | + // The target is the key under which the credentials will be stored. |
| 147 | + // It is recommended to set the target to something following the "Company_Application_Server" pattern. |
| 148 | + // Targets are per user, not per application, so using such a pattern will ensure uniqueness. |
| 149 | + dialog.Target = "Ookii_DialogsWpfSample_www.example.com"; |
| 150 | + if( dialog.ShowDialog(this) ) |
| 151 | + { |
| 152 | + MessageBox.Show(this, string.Format("You entered the following information:\nUser name: {0}\nPassword: {1}", dialog.Credentials.UserName, dialog.Credentials.Password), "Credential dialog sample"); |
| 153 | + // Normally, you should verify if the credentials are correct before calling ConfirmCredentials. |
| 154 | + // ConfirmCredentials will save the credentials if and only if the user checked the save checkbox. |
| 155 | + dialog.ConfirmCredentials(true); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private void ShowFolderBrowserDialog() |
| 161 | + { |
| 162 | + VistaFolderBrowserDialog dialog = new VistaFolderBrowserDialog(); |
| 163 | + dialog.Description = "Please select a folder."; |
| 164 | + dialog.UseDescriptionForTitle = true; // This applies to the Vista style dialog only, not the old dialog. |
| 165 | + if( !VistaFolderBrowserDialog.IsVistaFolderDialogSupported ) |
| 166 | + MessageBox.Show(this, "Because you are not using Windows Vista or later, the regular folder browser dialog will be used. Please use Windows Vista to see the new dialog.", "Sample folder browser dialog"); |
| 167 | + if( (bool)dialog.ShowDialog(this) ) |
| 168 | + MessageBox.Show(this, "The selected folder was: " + dialog.SelectedPath, "Sample folder browser dialog"); |
| 169 | + } |
| 170 | + |
| 171 | + private void ShowOpenFileDialog() |
| 172 | + { |
| 173 | + // As of .Net 3.5 SP1, WPF's Microsoft.Win32.OpenFileDialog class still uses the old style |
| 174 | + VistaOpenFileDialog dialog = new VistaOpenFileDialog(); |
| 175 | + dialog.Filter = "All files (*.*)|*.*"; |
| 176 | + if( !VistaFileDialog.IsVistaFileDialogSupported ) |
| 177 | + MessageBox.Show(this, "Because you are not using Windows Vista or later, the regular open file dialog will be used. Please use Windows Vista to see the new dialog.", "Sample open file dialog"); |
| 178 | + if( (bool)dialog.ShowDialog(this) ) |
| 179 | + MessageBox.Show(this, "The selected file was: " + dialog.FileName, "Sample open file dialog"); |
| 180 | + } |
| 181 | + |
| 182 | + private void ShowSaveFileDialog() |
| 183 | + { |
| 184 | + VistaSaveFileDialog dialog = new VistaSaveFileDialog(); |
| 185 | + dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; |
| 186 | + dialog.DefaultExt = "txt"; |
| 187 | + // As of .Net 3.5 SP1, WPF's Microsoft.Win32.SaveFileDialog class still uses the old style |
| 188 | + if( !VistaFileDialog.IsVistaFileDialogSupported ) |
| 189 | + MessageBox.Show(this, "Because you are not using Windows Vista or later, the regular save file dialog will be used. Please use Windows Vista to see the new dialog.", "Sample save file dialog"); |
| 190 | + if( (bool)dialog.ShowDialog(this) ) |
| 191 | + MessageBox.Show(this, "The selected file was: " + dialog.FileName, "Sample save file dialog"); |
| 192 | + } |
| 193 | + |
| 194 | + private void TaskDialog_HyperLinkClicked(object sender, HyperlinkClickedEventArgs e) |
| 195 | + { |
| 196 | + System.Diagnostics.Process.Start(e.Href); |
| 197 | + } |
| 198 | + |
| 199 | + private void _sampleProgressDialog_DoWork(object sender, DoWorkEventArgs e) |
| 200 | + { |
| 201 | + // Implement the operation that the progress bar is showing progress of here, same as you would do with a background worker. |
| 202 | + for( int x = 0; x <= 100; ++x ) |
| 203 | + { |
| 204 | + Thread.Sleep(500); |
| 205 | + // Periodically check CancellationPending and abort the operation if required. |
| 206 | + if( _sampleProgressDialog.CancellationPending ) |
| 207 | + return; |
| 208 | + // ReportProgress can also modify the main text and description; pass null to leave them unchanged. |
| 209 | + // If _sampleProgressDialog.ShowTimeRemaining is set to true, the time will automatically be calculated based on |
| 210 | + // the frequency of the calls to ReportProgress. |
| 211 | + _sampleProgressDialog.ReportProgress(x, null, string.Format(System.Globalization.CultureInfo.CurrentCulture, "Processing: {0}%", x)); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments