• WPF中通过MVVM模式来关闭View


    一,场景

        通过单击按钮执行ViewModel中的Command来关闭窗体,项目引用Prism4架构及使用MVVM进行UI层的开发。

    二, 实现代码(方式应该有很多种,现介绍两种我所想过的方式)

         (1).通过发布订阅事件来关闭窗体

         1.CodeBahind中的代码

     1   public partial class ApproveLoginView : Window
     2 
     3     {        
     4 
     5           [ImportingConstructor]         
     6 
     7           public ApproveLoginView()      
     8 
     9          {          
    10 
    11                   InitializeComponent();           
    12 
    13                   ServicesHelper.EventAggregator.GetEvent<CloseEvent>   ().Subscribe(CloseView);     
    14 
    15          }
    16 
    17         private void CloseView(object o)       
    18 
    19         {          
    20 
    21              if(o == DataContext)        
    22 
    23                     Close();           
    24 
    25         }     
    26 
    27 }
    View Code

         2.ViewModel部分代码

     1 public class ApproveLoginViewModel : NotificationObject, 
     2     {
     3        
     4         private IEventAggregator _eventAggregator;
     5 
     6         [ImportingConstructor]
     7         public ApproveLoginViewModel(IEventAggregator eventAggregator)
     8         {
     9             _eventAggregator = eventAggregator;
    10         }
    11 
    12         #region Command
    13 
    14         private ICommand _okCommand;
    15         public ICommand OkCommand
    16         {
    17             get
    18             {
    19                 if (_okCommand == null)
    20                 {
    21                     _okCommand = new RelayCommand(Ok);
    22                 }
    23                 return _okCommand;
    24             }
    25             private set { }
    26         }
    27 
    28        
    29         private void Ok()
    30         {
    31             _eventAggregator.GetEvent<CloseEvent>().Publish(this);
    32         }
    33 
    34         
    35         #endregion
    36     }
    View Code


         (2).通过Prism的interactionRequest来实现

           1.Window中的Xaml部分代码

     1 <Window x:Class="MVVMCloseView.MainWindow"
     2      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     3      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     4      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
     5      xmlns:v="http://schemas.microsoft.com/expression/2010/interactions"
     6      xmlns:prism="http://www.codeplex.com/prism"
     7         Title="MainWindow" Height="350" Width="525" x:Name="window">
     8     <i:Interaction.Triggers>
     9          <prism:InteractionRequestTrigger SourceObject="{Binding   CloseRequest}">
    10               <v:CallMethodAction MethodName="Close" TargetObject="{Binding ElementName=window}"/>
    11         </prism:InteractionRequestTrigger>
    12     </i:Interaction.Triggers>
    13     <Grid>
    14             <Button Content="Close" Grid.Row="1" FontSize="30" Width="200" Height="100" Command="{Binding CloseCommand}"/>
    15     </Grid>
    16 </Window>
    View Code

           2.ViewModel中的部分代码
     

     1  public class MainWindowViewModel : NotificationObject
     2     {
     3         public MainWindowViewModel()
     4         {
     5             this.CloseCommand = new DelegateCommand(new Action(Close));
     6             this.CloseRequest = new InteractionRequest<Notification>();
     7         }
     8 
     9         public DelegateCommand CloseCommand { get; set; }
    10 
    11         private void Close()
    12         {
    13             this.CloseRequest.Raise(new Notification() );
    14         }
    15 
    16         public InteractionRequest<Notification> CloseRequest { get; set; }
    17     }
    View Code

    好了,就说到这了,因为自己在通过ViewModel来关闭view碰到些问题,上网找了些资料后找到这些解决方法的,希望对有需要的朋友能有帮助。

  • 相关阅读:
    智能佳 金刚足球机器人 竞赛机器人 智能机器人
    DIY小能手|别买电动滑板车了,咱做一台吧
    !!2016/02/22——当日买入——事后追悔,总结经验,忘记了买票的初衷!
    20160222深夜 支撑与阻力的问题,突破要不要买,回踩要不要接
    2016/2/4——昨天操作错误
    C语言 · 瓷砖铺放
    C语言 · 字符串编辑
    C语言 · 比较字符串
    C语言 · 3000米排名预测
    C语言 · 陶陶摘苹果2
  • 原文地址:https://www.cnblogs.com/JustYong/p/3108406.html
Copyright © 2020-2023  润新知