gpt4 book ai didi

wpf - 单元测试附加行为 wpf

转载 作者:行者123 更新时间:2023-12-04 11:45:25 24 4
gpt4 key购买 nike

我仍然在探索一般的附加行为,并且不知如何为一个行为编写单元测试。

我在下面粘贴了 Sacha Barber 的 Cinch 框架中的一些代码,该框架允许通过附加行为关闭窗口。有人可以给我看一个示例单元测试吗?

谢谢!
贝瑞尔

    #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

最佳答案

您可能会在 ICommand 中使用 lambda使用 DelegateCommandRelayCommand .这些的多种实现无处不在,Cinch 可能有类似的东西。非常简单的版本(例如,不适合生产使用):

public class DelegateCommand : ICommand {
private Action _execute = null;

public void Execute( object parameter ) {
_execute();
}

public DelegateCommand( Action execute ) {
_execute = execute;
}

#region stuff that doesn't affect functionality
public bool CanExecute( object parameter ) {
return true;
}
public event EventHandler CanExecuteChanged {
add { }
remove { }
}
#endregion
}

那么你的测试体可能看起来像这样:
bool wascalled = false;

var execute = new DelegateCommand(
() => {
wascalled = true;
} );

var window = new Window();
SomeClass.SetClose( window, execute );

// does the window need to be shown for Close() to work? Nope.

window.Close();

AssertIsTrue( wascalled );

这是一个过于简化的例子。当然,您还需要执行其他测试,在这种情况下,您应该创建或找到更完整的 DelegateCommand 实现。也正确实现 CanExecute , 除其他事项外。

关于wpf - 单元测试附加行为 wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2297072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com