MVVM - 事件

事件是一种编程结构,它对状态的变化作出反应,通知任何已注册通知的端点.主要地,事件用于通过鼠标和键盘通知用户输入,但是它们的用途不限于此.每当检测到状态更改时,可能在加载或初始化对象时,可以触发事件以提醒任何感兴趣的第三方.

  • 在使用MVVM(Model-View-ViewModel)设计模式的WPF应用程序中,视图模型是负责处理应用程序的表示逻辑和状态的组件.

  • 视图的代码隐藏文件不应包含任何代码来处理从任何用户界面(UI)元素(如Button或ComboBox)引发的事件,也不应包含任何特定于域的逻辑./p>

  • 理想情况下,View的代码隐藏只包含一个调用InitializeComponent方法的构造函数,也许还有一些额外的代码来控制或与视图层交互在XAML中表达困难或低效,例如复杂的动画.

让我们看看我们的应用程序中按钮点击事件的一个简单示例.以下是MainWindow.xaml文件的XAML代码,您将在其中看到两个按钮.

<Window x:Class = "MVVMHierarchiesDemo.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:local = "clr-namespace:MVVMHierarchiesDemo" 
   xmlns:views = "clr-namespace:MVVMHierarchiesDemo.Views" 
   xmlns:viewModels = "clr-namespace:MVVMHierarchiesDemo.ViewModel" 
   mc:Ignorable = "d" 
   Title = "MainWindow" Height = "350" Width = "525">
	
   <Window.DataContext> 
      <local:MainWindowViewModel/> 
   </Window.DataContext>
	
   <Window.Resources> 
      <DataTemplate DataType = "{x:Type viewModels:CustomerListViewModel}">
         <views:CustomerListView/> 
      </DataTemplate>
		 
      <DataTemplate DataType = "{x:Type viewModels:OrderViewModel}">
         <views:OrderView/>
      </DataTemplate> 
   </Window.Resources> 

   <Grid> 
      <Grid.RowDefinitions> 
         <RowDefinition Height = "Auto" /> 
         <RowDefinition Height = "*" /> 
      </Grid.RowDefinitions> 
		
      <Grid x:Name = "NavBar"> 
         <Grid.ColumnDefinitions> 
            <ColumnDefinition Width = "*" />
            <ColumnDefinition Width = "*" /> 
            <ColumnDefinition Width = "*" /> 
         </Grid.ColumnDefinitions>
			
         <Button Content = "Customers" 
            Command = "{Binding NavCommand}" 
            CommandParameter = "customers" 
            Grid.Column = "0" />
				
         <Button Content = "Order" 
            Command = "{Binding NavCommand}" 
            CommandParameter = "orders" 
            Grid.Column = "2" />
      </Grid>
		
      <Grid x:Name = "MainContent" Grid.Row = "1"> 
         <ContentControl Content = "{Binding CurrentViewModel}" />
      </Grid> 
		
   </Grid> 

</Window>

您可以看到上面的XAML文件中没有使用按钮Click属性,但按下按钮时Command和CommandParameter属性用于加载不同的视图.现在,您需要在MainWindowViewModel.cs文件中定义命令实现,但不要在View文件中定义.以下是完整的MainWindowViewModel实现.

using MVVMHierarchiesDemo.ViewModel; 
using MVVMHierarchiesDemo.Views; 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks;

namespace MVVMHierarchiesDemo { 

   class MainWindowViewModel : BindableBase { 
	
      public MainWindowViewModel() { 
         NavCommand = new MyICommand<string>(OnNav); 
      } 

      private CustomerListViewModel custListViewModel = new CustomerListViewModel(); 
      private OrderViewModel orderViewModelModel = new OrderViewModel();

      private BindableBase _CurrentViewModel; 
		
      public BindableBase CurrentViewModel { 
         get { return _CurrentViewModel; } 
         set { SetProperty(ref _CurrentViewModel, value); } 
      } 
		
      public MyICommand<string> NavCommand { get; private set; }

      private void OnNav(string destination) { 
		
         switch (destination) { 
            case "orders": 
               CurrentViewModel = orderViewModelModel; 
               break; 
            case "customers":
               default: 
               CurrentViewModel = custListViewModel; 
               break; 
         } 
      } 
   } 
}

从BindableBase类派生所有ViewModel.编译并执行上面的代码时,您将看到以下输出.

MVVM Events MainWindow1

如您所见,我们在MainWindow上只添加了两个按钮和一个CurrentViewModel.现在,如果您单击任何按钮,它将导航到该特定视图.让我们点击Customers按钮,你会看到显示了CustomerListView.

MVVM Events MainWindow2

我们建议您逐步执行上述示例,以便更好地理解.