NetworkListView.xaml.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Timers;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using System.Windows.Threading;
  20. namespace WinUI
  21. {
  22. /// <summary>
  23. /// Interaction logic for MainWindow.xaml
  24. /// </summary>
  25. public partial class NetworkListView : Window
  26. {
  27. Regex charRegex = new Regex("[0-9a-fxA-FX]");
  28. Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
  29. public NetworkListView()
  30. {
  31. InitializeComponent();
  32. Closed += onClosed;
  33. NetworkMonitor.Instance.SubscribeNetworkUpdates(updateNetworks);
  34. }
  35. ~NetworkListView()
  36. {
  37. }
  38. private void onClosed(object sender, System.EventArgs e)
  39. {
  40. NetworkMonitor.Instance.UnsubscribeNetworkUpdates(updateNetworks);
  41. }
  42. private void updateNetworks(List<ZeroTierNetwork> networks)
  43. {
  44. if (networks != null)
  45. {
  46. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  47. {
  48. networksPage.setNetworks(networks);
  49. }));
  50. }
  51. }
  52. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  53. {
  54. e.Handled = !charRegex.IsMatch(e.Text);
  55. }
  56. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  57. {
  58. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  59. if (!isText) return;
  60. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  61. if (!wholeStringRegex.IsMatch(text))
  62. {
  63. e.CancelCommand();
  64. }
  65. }
  66. }
  67. }