From 719653079a1df3f9e6c7fc518b67d83cdca5ee35 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Wed, 21 May 2014 11:32:51 +0100 Subject: [PATCH] Use MessageBox in the integration tests and samples --- .../Stylet.Samples.Hello/ShellViewModel.cs | 7 +- .../Pages/ShellViewModel.cs | 5 -- .../Pages/UserViewModel.cs | 7 +- .../Xaml/Secure.cs | 82 +++++++++++++++---- Stylet/MessageBox.cs | 2 +- StyletIntegrationTests/ShellViewModel.cs | 6 +- .../WindowLifecycle/WindowViewModel.cs | 8 +- 7 files changed, 84 insertions(+), 33 deletions(-) diff --git a/Samples/Stylet.Samples.Hello/ShellViewModel.cs b/Samples/Stylet.Samples.Hello/ShellViewModel.cs index df3b723..4201b67 100644 --- a/Samples/Stylet.Samples.Hello/ShellViewModel.cs +++ b/Samples/Stylet.Samples.Hello/ShellViewModel.cs @@ -9,6 +9,8 @@ namespace Stylet.Samples.Hello { class ShellViewModel : Screen { + private IWindowManager windowManager; + private string _name; public string Name { @@ -16,9 +18,10 @@ namespace Stylet.Samples.Hello set { SetAndNotify(ref this._name, value); this.NotifyOfPropertyChange(() => this.CanSayHello); } } - public ShellViewModel() + public ShellViewModel(IWindowManager windowManager) { this.DisplayName = "Hello, Stylet"; + this.windowManager = windowManager; } public bool CanSayHello @@ -27,7 +30,7 @@ namespace Stylet.Samples.Hello } public void SayHello() { - MessageBox.Show(String.Format("Hello, {0}", this.Name)); // Don't do this + this.windowManager.ShowMessageBox(String.Format("Hello, {0}", this.Name)); } } } diff --git a/Samples/Stylet.Samples.ModelValidation/Pages/ShellViewModel.cs b/Samples/Stylet.Samples.ModelValidation/Pages/ShellViewModel.cs index 5c6b3b9..aa12679 100644 --- a/Samples/Stylet.Samples.ModelValidation/Pages/ShellViewModel.cs +++ b/Samples/Stylet.Samples.ModelValidation/Pages/ShellViewModel.cs @@ -8,11 +8,6 @@ namespace Stylet.Samples.ModelValidation.Pages { public class ShellViewModel : Conductor { - public ShellViewModel() - { - this.ActiveItem = new UserViewModel(new FluentModelValidator(new UserViewModelValidator())); - } - public ShellViewModel(UserViewModel userViewModel) { this.DisplayName = "Stylet.Samples.ModelValidation"; diff --git a/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs b/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs index 072ee34..8fd332f 100644 --- a/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs +++ b/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs @@ -9,6 +9,8 @@ namespace Stylet.Samples.ModelValidation.Pages { public class UserViewModel : Screen { + private IWindowManager windowManager; + public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } @@ -21,8 +23,9 @@ namespace Stylet.Samples.ModelValidation.Pages set { this.autoValidate = value; this.NotifyOfPropertyChange(); } } - public UserViewModel(IModelValidator validator) : base(validator) + public UserViewModel(IWindowManager windowManager, IModelValidator validator) : base(validator) { + this.windowManager = windowManager; } protected override void OnValidationStateChanged(IEnumerable changedProperties) @@ -39,7 +42,7 @@ namespace Stylet.Samples.ModelValidation.Pages public void Submit() { if (this.Validate()) - System.Windows.MessageBox.Show("Successfully submitted"); + this.windowManager.ShowMessageBox("Successfully submitted", "success"); } } diff --git a/Samples/Stylet.Samples.ModelValidation/Xaml/Secure.cs b/Samples/Stylet.Samples.ModelValidation/Xaml/Secure.cs index 65b3d54..c88e228 100644 --- a/Samples/Stylet.Samples.ModelValidation/Xaml/Secure.cs +++ b/Samples/Stylet.Samples.ModelValidation/Xaml/Secure.cs @@ -12,8 +12,30 @@ namespace Stylet.Samples.ModelValidation.Xaml { public static class Secure { - private static bool passwordInitialized; - private static bool settingPassword; + private static bool GetPasswordInitialized(DependencyObject obj) + { + return (bool)obj.GetValue(PasswordInitializedProperty); + } + private static void SetPasswordInitialized(DependencyObject obj, bool value) + { + obj.SetValue(PasswordInitializedProperty, value); + } + // Using a DependencyProperty as the backing store for PasswordInitialized. This enables animation, styling, binding, etc... + private static readonly DependencyProperty PasswordInitializedProperty = + DependencyProperty.RegisterAttached("PasswordInitialized", typeof(bool), typeof(Secure), new PropertyMetadata(false)); + + + private static bool GetSettingPassword(DependencyObject obj) + { + return (bool)obj.GetValue(SettingPasswordProperty); + } + private static void SetSettingPassword(DependencyObject obj, bool value) + { + obj.SetValue(SettingPasswordProperty, value); + } + // Using a DependencyProperty as the backing store for SettingPassword. This enables animation, styling, binding, etc... + private static readonly DependencyProperty SettingPasswordProperty = + DependencyProperty.RegisterAttached("SettingPassword", typeof(bool), typeof(Secure), new PropertyMetadata(false)); public static string GetPassword(DependencyObject obj) { @@ -39,17 +61,17 @@ namespace Stylet.Samples.ModelValidation.Xaml private static void HandleBoundPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e) { - if (settingPassword) - return; - var passwordBox = dp as PasswordBox; if (passwordBox == null) return; + if (GetSettingPassword(passwordBox)) + return; + // If this is the initial set - if (!passwordInitialized) + if (!GetPasswordInitialized(passwordBox)) { - passwordInitialized = true; + SetPasswordInitialized(passwordBox, true); passwordBox.PasswordChanged += HandlePasswordChanged; } @@ -59,14 +81,38 @@ namespace Stylet.Samples.ModelValidation.Xaml private static void HandlePasswordChanged(object sender, RoutedEventArgs e) { var passwordBox = (PasswordBox)sender; - settingPassword = true; + SetSettingPassword(passwordBox, true); SetPassword(passwordBox, passwordBox.Password); - settingPassword = false; + SetSettingPassword(passwordBox, false); } - private static bool securePasswordInitialized; - private static bool settingSecurePassword; + private static bool GetSecurePasswordInitialized(DependencyObject obj) + { + return (bool )obj.GetValue(SecurePasswordInitializedProperty); + } + private static void SetSecurePasswordInitialized(DependencyObject obj, bool value) + { + obj.SetValue(SecurePasswordInitializedProperty, value); + } + // Using a DependencyProperty as the backing store for SecurePasswordInitialized. This enables animation, styling, binding, etc... + private static readonly DependencyProperty SecurePasswordInitializedProperty = + DependencyProperty.RegisterAttached("SecurePasswordInitialized", typeof(bool ), typeof(Secure), new PropertyMetadata(false)); + + private static bool GetSettingSecurePassword(DependencyObject obj) + { + return (bool)obj.GetValue(SettingSecurePasswordProperty); + } + + private static void SetSettingSecurePassword(DependencyObject obj, bool value) + { + obj.SetValue(SettingSecurePasswordProperty, value); + } + + // Using a DependencyProperty as the backing store for SettingSecurePassword. This enables animation, styling, binding, etc... + private static readonly DependencyProperty SettingSecurePasswordProperty = + DependencyProperty.RegisterAttached("SettingSecurePassword", typeof(bool), typeof(Secure), new PropertyMetadata(false)); + public static SecureString GetSecurePassword(DependencyObject obj) { @@ -89,26 +135,26 @@ namespace Stylet.Samples.ModelValidation.Xaml private static void HandleBoundSecurePasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e) { - if (settingSecurePassword) - return; - var passwordBox = dp as PasswordBox; if (passwordBox == null) return; - if (!securePasswordInitialized) + if (GetSettingSecurePassword(passwordBox)) + return; + + if (!GetSecurePasswordInitialized(passwordBox)) { passwordBox.PasswordChanged += HandleSecurePasswordChanged; - securePasswordInitialized = true; + SetSecurePasswordInitialized(passwordBox, true); } } private static void HandleSecurePasswordChanged(object sender, RoutedEventArgs e) { var passwordBox = (PasswordBox)sender; - settingSecurePassword = true; + SetSettingSecurePassword(passwordBox, true); SetSecurePassword(passwordBox, passwordBox.SecurePassword); - settingSecurePassword = false; + SetSettingSecurePassword(passwordBox, false); } } } diff --git a/Stylet/MessageBox.cs b/Stylet/MessageBox.cs index b0ecb1e..f58ed60 100644 --- a/Stylet/MessageBox.cs +++ b/Stylet/MessageBox.cs @@ -26,7 +26,7 @@ namespace Stylet /// Button pressed when the user presses Enter. Defaults to the leftmost button /// Button pressed when the user preses Esc or clicks the red X on the titlebar. Defaults to the rightmost button /// Which button the user clicked - public static MessageBoxResult ShowMessageBox(this IWindowManager windowManager, string text, string title, MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultButton = MessageBoxResult.None, MessageBoxResult cancelButton = MessageBoxResult.None) + public static MessageBoxResult ShowMessageBox(this IWindowManager windowManager, string text, string title = null, MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultButton = MessageBoxResult.None, MessageBoxResult cancelButton = MessageBoxResult.None) { var vm = IoC.Get(); vm.Setup(text, title, buttons, icon, defaultButton, cancelButton); diff --git a/StyletIntegrationTests/ShellViewModel.cs b/StyletIntegrationTests/ShellViewModel.cs index 840cb77..7b12113 100644 --- a/StyletIntegrationTests/ShellViewModel.cs +++ b/StyletIntegrationTests/ShellViewModel.cs @@ -35,7 +35,7 @@ namespace StyletIntegrationTests public void ShowWindowLifecycle() { - var window = new WindowLifecycle.WindowViewModel(); + var window = new WindowLifecycle.WindowViewModel(this.windowManager); this.windowManager.ShowWindow(window); } @@ -60,9 +60,9 @@ namespace StyletIntegrationTests await Task.Delay(100); if (log.SequenceEqual(new[] { "One", "Two", "Four", "Three" })) - MessageBox.Show("Success"); + this.windowManager.ShowMessageBox("Success"); else - MessageBox.Show("Failure"); + this.windowManager.ShowMessageBox("Failure"); } } } diff --git a/StyletIntegrationTests/WindowLifecycle/WindowViewModel.cs b/StyletIntegrationTests/WindowLifecycle/WindowViewModel.cs index b1f0599..b7ce890 100644 --- a/StyletIntegrationTests/WindowLifecycle/WindowViewModel.cs +++ b/StyletIntegrationTests/WindowLifecycle/WindowViewModel.cs @@ -10,12 +10,16 @@ namespace StyletIntegrationTests.WindowLifecycle { public class WindowViewModel : Screen { + private IWindowManager windowManager; + public BindableCollection Log { get; private set; } - public WindowViewModel() + public WindowViewModel(IWindowManager windowManager) { this.DisplayName = "Window Lifecycle"; + this.windowManager = windowManager; + this.Log = new BindableCollection(); } @@ -41,7 +45,7 @@ namespace StyletIntegrationTests.WindowLifecycle protected override void OnClose() { - MessageBox.Show("Closed"); + this.windowManager.ShowMessageBox("Closed", "Closed"); } } }