假设我有以下 XAML :
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Button Content="Write something" Canvas.Left="43" Canvas.Top="159" Width="162" Height="42" Click="Button_Click_1"/>
</Canvas>
我的 ViewModel 可以是此类,也可以包含它的实例(视图模型逻辑):
//Here is my static class for extension methods
public static class ExtendenWindowClass
{
/// <summary>
/// Eventhandler for Button
/// </summary>
/// <param name="obj"></param>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void Button_Click_1(this MainWindow obj, object sender, RoutedEventArgs e)
{
MessageBox.Show("Wait 10 seconds");
Thread.Sleep(10000);
MessageBox.Show("Ready, now you can press again");
}
}
因此,嗡嗡声不再在代码后面,而是在扩展方法中。MainWindow 类的静态字段的使用很少,因此可以跳过。
xaml 的外观比 DataBinding 对象和花括号更自然。而且我也遵循概念分离。你怎么看 ?