/Synotune/Views/SettingsView.xaml.cs

https://github.com/salfab/Synotune · C# · 182 lines · 131 code · 44 blank · 7 comment · 14 complexity · 773c04f2caab3ad15f13669a3175e8a8 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Windows.Foundation;
  6. using Windows.Graphics.Display;
  7. using Windows.UI.ViewManagement;
  8. using Windows.UI.Xaml;
  9. using Windows.UI.Xaml.Controls;
  10. using Windows.UI.Xaml.Data;
  11. using Windows.UI.Xaml.Input;
  12. namespace Synotune.Views
  13. {
  14. public sealed partial class SettingsView
  15. {
  16. public SettingsView()
  17. {
  18. InitializeComponent();
  19. this.Loaded += (s, ea) =>
  20. {
  21. if (LoadingDefaultSettings != null)
  22. {
  23. var potentialOverride = new LoadingDefaultSettingsEventArgs();
  24. LoadingDefaultSettings(this, potentialOverride);
  25. this.Username = potentialOverride.Username;
  26. this.Password = potentialOverride.Password;
  27. this.Hostname = potentialOverride.Hostname;
  28. this.Port = potentialOverride.Port;
  29. }
  30. };
  31. }
  32. public event EventHandler<LoadingDefaultSettingsEventArgs> LoadingDefaultSettings;
  33. public ICommand SettingsChanged
  34. {
  35. get { return (ICommand)GetValue(SettingsChangedProperty); }
  36. set { SetValue(SettingsChangedProperty, value); }
  37. }
  38. // Using a DependencyProperty as the backing store for SettingsChanged. This enables animation, styling, binding, etc...
  39. public static readonly DependencyProperty SettingsChangedProperty =
  40. DependencyProperty.Register("SettingsChanged", "Object", typeof(SettingsView).FullName, new PropertyMetadata(null));
  41. public string Username
  42. {
  43. get { return (string)GetValue(UsernameProperty); }
  44. set { SetValue(UsernameProperty, value); }
  45. }
  46. // Using a DependencyProperty as the backing store for Username. This enables animation, styling, binding, etc...
  47. public static readonly DependencyProperty UsernameProperty =
  48. DependencyProperty.Register("Username", "Object", typeof(SettingsView).FullName, new PropertyMetadata(null, OnSettingsChanged ));
  49. private static void OnSettingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  50. {
  51. var settingsChangedCommand = (DelegateCommand<SettingsView>) ((SettingsView) d).SettingsChanged;
  52. if (settingsChangedCommand != null)
  53. {
  54. if (settingsChangedCommand.CanExecute(d))
  55. {
  56. settingsChangedCommand.Execute(d);
  57. }
  58. }
  59. }
  60. public string Password
  61. {
  62. get { return (string)GetValue(PasswordProperty); }
  63. set { SetValue(PasswordProperty, value); }
  64. }
  65. // Using a DependencyProperty as the backing store for Password. This enables animation, styling, binding, etc...
  66. public static readonly DependencyProperty PasswordProperty =
  67. DependencyProperty.Register("Password", "Object", typeof(SettingsView).ToString(), new PropertyMetadata(null, OnSettingsChanged));
  68. public string Hostname
  69. {
  70. get { return (string)GetValue(HostnameProperty); }
  71. set { SetValue(HostnameProperty, value); }
  72. }
  73. // Using a DependencyProperty as the backing store for Hostname. This enables animation, styling, binding, etc...
  74. public static readonly DependencyProperty HostnameProperty =
  75. DependencyProperty.Register("Hostname", "Object", typeof(SettingsView).ToString(), new PropertyMetadata(null, OnSettingsChanged));
  76. public string Port
  77. {
  78. get { return (string)GetValue(PortProperty); }
  79. set { SetValue(PortProperty, value); }
  80. }
  81. // Using a DependencyProperty as the backing store for Port. This enables animation, styling, binding, etc...
  82. public static readonly DependencyProperty PortProperty =
  83. DependencyProperty.Register("Port", "Object", typeof(SettingsView).FullName, new PropertyMetadata(null, OnSettingsChanged));
  84. protected override void OnPointerPressed(Windows.UI.Xaml.Input.PointerEventArgs e)
  85. {
  86. this.Focus();
  87. base.OnPointerPressed(e);
  88. e.Handled = true;
  89. }
  90. // View state management for switching among Full, Fill, Snapped, and Portrait states
  91. private DisplayPropertiesEventHandler _displayHandler;
  92. private TypedEventHandler<ApplicationLayout, ApplicationLayoutChangedEventArgs> _layoutHandler;
  93. private FrameworkElement lastFocusedTextbox;
  94. private void Page_Loaded(object sender, RoutedEventArgs e)
  95. {
  96. if (_displayHandler == null)
  97. {
  98. _displayHandler = Page_OrientationChanged;
  99. _layoutHandler = Page_LayoutChanged;
  100. }
  101. DisplayProperties.OrientationChanged += _displayHandler;
  102. ApplicationLayout.GetForCurrentView().LayoutChanged += _layoutHandler;
  103. SetCurrentOrientation(this);
  104. }
  105. private void Page_Unloaded(object sender, RoutedEventArgs e)
  106. {
  107. DisplayProperties.OrientationChanged -= _displayHandler;
  108. ApplicationLayout.GetForCurrentView().LayoutChanged -= _layoutHandler;
  109. }
  110. private void Page_LayoutChanged(object sender, ApplicationLayoutChangedEventArgs e)
  111. {
  112. SetCurrentOrientation(this);
  113. }
  114. private void Page_OrientationChanged(object sender)
  115. {
  116. SetCurrentOrientation(this);
  117. }
  118. private void SetCurrentOrientation(Control viewStateAwareControl)
  119. {
  120. VisualStateManager.GoToState(viewStateAwareControl, this.GetViewState(), false);
  121. }
  122. private String GetViewState()
  123. {
  124. var orientation = DisplayProperties.CurrentOrientation;
  125. if (orientation == DisplayOrientations.Portrait ||
  126. orientation == DisplayOrientations.PortraitFlipped) return "Portrait";
  127. var layout = ApplicationLayout.Value;
  128. if (layout == ApplicationLayoutState.Filled) return "Fill";
  129. if (layout == ApplicationLayoutState.Snapped) return "Snapped";
  130. return "Full";
  131. }
  132. private void TextBox_GotFocus(object sender, RoutedEventArgs e)
  133. {
  134. // FocusManager.GetFocusedElement does not exist in the developer preview... ( http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/94ccb346-2e4d-426b-8324-3ba7bbcd24fe )
  135. this.lastFocusedTextbox = (FrameworkElement)sender;
  136. }
  137. }
  138. }