我先把代码放出来,你们可以自己下载下来看看。之前我们讲了简单的MVVM,但是我们还是在前台写代码了;
比如:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes;
namespace SimpleCal { public partial class MainPage : UserControl { //ViewModel.CalViewModel calView = new ViewModel.CalViewModel(); //实例化一个类,我们也可以写到XAML中 public MainPage() { InitializeComponent(); } private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) { //LayoutRoot.DataContext = calView; // btnSum.Command = new ViewModel.Addcommand(); // btnSum.CommandParameter = calView.Cal; }
private void btnSum_Click(object sender, RoutedEventArgs e) { // calView.Cal.Result = calView.Cal.Number1 + calView.Cal.Number2; }
} }
===================================================================
下面我们就把原来的这些代码注释掉,把逻辑代码写到viewmodel里面。
首先,我们的button都有一个Command属性跟CommandParameter属性,
是继承自ICommand接口的,那么我们在原来的CalViewModel.cs类中加一个类,
并且实现ICommand接口,代码如下:
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using SimpleCal.Model;
namespace SimpleCal.ViewModel { public class CalViewModel { CalMode cal = new CalMode() { Number1 = 0, Number2 = 0, Result = 0 };
public CalMode Cal { get { return cal; } set { cal = value; } }
}
public class Addcommand : ICommand {
public bool CanExecute(object parameter) { return true; }
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) { //MessageBox.Show("被执行了!"); Model.CalMode model = parameter as Model.CalMode; //MessageBox.Show(string.Format("{0}+{1}",model.Number1,model.Number2)); model.Result = model.Number1 + model.Number2;
} } }
=======================================================================
那么现在就同样可以实现计算的功能了,然后再看我们MainPage.xaml.cs的代码
其中实例化一个类,并且给LayoutRoot.DataContext赋值,btnSum.Command赋值,btnSum.CommandParameter赋值,其实这些都是可以在前台的xaml中完成的,那么完整的前台代码如下:
=================================================================================
<UserControl x:Class="SimpleCal.MainPage" xmlns="" xmlns:x="" xmlns:d="" xmlns:local="clr-namespace:SimpleCal.ViewModel" xmlns:mc="" mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="600"> <UserControl.Resources> <local:CalViewModel x:Key="myViewModel"></local:CalViewModel> <local:Addcommand x:Key="myAddCommond"></local:Addcommand> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="Green" Loaded="LayoutRoot_Loaded" DataContext="{StaticResource myViewModel }"> <TextBox Name="tbNum1" Text="{Binding Cal.Number1,Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="6,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <TextBox Name="tbNum2" Text="{Binding Cal.Number2, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="162,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <TextBox Name="tbResult" Text="{Binding Cal.Result,Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="385,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <TextBlock HorizontalAlignment="Left" Margin="139,50,0,0" TextWrapping="Wrap" Text="+" VerticalAlignment="Top"/> <Button Content="=" HorizontalAlignment="Left" Margin="299,45,0,0" VerticalAlignment="Top" Width="75" Name="btnSum" Click="btnSum_Click" Command="{StaticResource myAddCommond }" CommandParameter="{Binding Cal}" />
</Grid> </UserControl>
=====================================================================
OK,大功告成。
原文链接: