Je suis encore en train de grokking des comportements attachés en général, et je ne sais pas comment écrire un test unitaire.unité test un comportement ci-joint wpf
J'ai collé du code ci-dessous dans la structure Cinch de Sacha Barber qui permet à une fenêtre d'être fermée via un comportement attaché. Quelqu'un peut-il me montrer un exemple de test unitaire?
Merci!
Berryl
#region Close
/// <summary>Dependency property which holds the ICommand for the Close event</summary>
public static readonly DependencyProperty CloseProperty =
DependencyProperty.RegisterAttached("Close",
typeof(ICommand), typeof(Lifetime),
new UIPropertyMetadata(null, OnCloseEventInfoChanged));
/// <summary>Attached Property getter to retrieve the CloseProperty ICommand</summary>
public static ICommand GetClose(DependencyObject source)
{
return (ICommand)source.GetValue(CloseProperty);
}
/// <summary>Attached Property setter to change the CloseProperty ICommand</summary>
public static void SetClose(DependencyObject source, ICommand command)
{
source.SetValue(CloseProperty, command);
}
/// <summary>This is the property changed handler for the Close property.</summary>
private static void OnCloseEventInfoChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var win = sender as Window;
if (win == null) return;
win.Closing -= OnWindowClosing;
win.Closed -= OnWindowClosed;
if (e.NewValue == null) return;
win.Closing += OnWindowClosing;
win.Closed += OnWindowClosed;
}
/// <summary>
/// This method is invoked when the Window.Closing event is raised.
/// It checks with the ICommand.CanExecute handler
/// and cancels the event if the handler returns false.
/// </summary>
private static void OnWindowClosing(object sender, CancelEventArgs e)
{
var dpo = (DependencyObject)sender;
var ic = GetClose(dpo);
if (ic == null) return;
e.Cancel = !ic.CanExecute(GetCommandParameter(dpo));
}
/// <summary>
/// This method is invoked when the Window.Closed event is raised.
/// It executes the ICommand.Execute handler.
/// </summary>
static void OnWindowClosed(object sender, EventArgs e)
{
var dpo = (DependencyObject)sender;
var ic = GetClose(dpo);
if (ic == null) return;
ic.Execute(GetCommandParameter(dpo));
}
#endregion
Cinch va encore plus loin avec RelayCommand en faisant cuire votre test wascalled dans une propriété CommandSucceeded bool. Votre message est utile pour faire en sorte que SetClose soit toujours un accesseur de propriété à la fin de la journée, même s'il ne ressemble pas à l'ensemble des paramètres normaux de la propriété C#! C'est l'une des choses que je ne voyais pas et je ne suis pas encore intuitif sur le comportement DP/attaché. Cheers – Berryl
Oui. Une fois compilées, ces méthodes Get/Set statiques sont appelées. Même chose avec les DP: il ignore l'enveloppe des propriétés et appelle directement SetValue'/'GetValue' sur' DependencyObject'. C'est bon d'entendre ça à Cinch. Ce n'est pas celui que j'ai déjà lu. –