在尝试使用 WPF 跟踪 MVVM 时,我遇到了一个我不确定将代码放在哪里进行测试的情况。btnReset_Click 方法 UI 命令中的所有行,所以我不知道它们是否属于视图模型,但我也不想将代码放在我的代码后面。我列出了我想到的两个选项,但也对另一种解决方案持开放态度。
Public class Main : Window
{
private void btnReset_Click(object sender, RoutedEventArgs e)
{
DynamicStackPanel.Children.Clear();
controls.Clear();
txtUsername.Text = string.Empty;
txtPassword.Text = string.Empty;
txtResponse.Text = string.Empty;
cbxTestEnvironment.SelectedValue = string.Empty;
cbxOperation.SelectedItem = null;
cbxTestEnvironment.SelectedItem = null;
}
}
或者
public class MainViewModel : DependencyObject
{
public MainViewModel : DependancyObject
{
public MainViewModel(UtilityMain win)
{
win.btnReset.Click +=btnReset_Click;
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
DynamicStackPanel.Children.Clear();
controls.Clear();
txtUsername.Text = string.Empty;
txtPassword.Text = string.Empty;
txtResponse.Text = string.Empty;
cbxTestEnvironment.SelectedValue = string.Empty;
cbxOperation.SelectedItem = null;
cbxTestEnvironment.SelectedItem = null;
}
}