• MVVM开发模式MVVM Light Toolkit中使用事件和参数传递


    Light中定义了类GalaSoft.MvvmLight.Command.RelayCommand

    这个类继承了ICommand方法,实现了其中的方法,Action就是一个方法参数

    复制代码
    // 摘要:     //     A command whose sole purpose is to relay its functionality to other objects     //     by invoking delegates. The default return value for the CanExecute method     //     is 'true'. This class does not allow you to accept command parameters in     //     the Execute and CanExecute callback methods.     public class RelayCommand : ICommand     {         // 摘要:         //     Initializes a new instance of the RelayCommand class that can always execute.          public RelayCommand(Action execute);         //         // 摘要:         //     Initializes a new instance of the RelayCommand class.          public RelayCommand(Action execute, Func<bool> canExecute);          // 摘要:         //     Occurs when changes occur that affect whether the command should execute.         public event EventHandler CanExecuteChanged;          // 摘要:         //     Defines the method that determines whether the command can execute in its         //     current state.          [DebuggerStepThrough]         public bool CanExecute(object parameter);         //         // 摘要:         //     Defines the method to be called when the command is invoked.          public void RaiseCanExecuteChanged();     }
    复制代码

    做一个简单的导航事件

    从MainView导航到View1,

    在View1的Viewmodel中定义这个事件属性

     public RelayCommand GotoView1
            {
                get;
                set;
            }

    在构造函数中赋值给GotoView1

     GotoView1 = new RelayCommand(delegate {
                    System.Windows.MessageBox.Show("Go to View1");
                });

    //或者定义一个单独的方法  GotoView1 = new RelayCommand( ()=>GoToView1());

    然后在MainPage.xaml中绑定事件

      <HyperlinkButton x:Name="View1" Grid.Row="1" Content="Go to View1" Command="{Binding GotoView1}" ></HyperlinkButton>

     

    参数传递。

    定义事件的时候很多情况下都需要传入参数,MVVM light也有这功能,而且 比较简单,方式如下:

    //<string>这个就是GotoView1方法需要的参数,参数类型是string,当然可以根据需要变成其他类型,实体类型都可以
    复制代码

    public RelayCommand<string> GotoView1 { get; set; }

      public void GoToView11(string msg)
      {
             System.Windows.MessageBox.Show(msg + "Go to View1");
      }

    //对这个方法属性赋值,test只是形式参数,无任何意义,意思是把字符串类型的test传递给GotoView方法
    GotoView1 = new RelayCommand<string>((test) => GoToView11(test));


    复制代码

    页面上绑定这个方法的时候可以指定CommandParameter来指定Command的绑定方法的参数,如果类型是字符串,直接="字符串"就可以

    如果是其他实体类型可以参考使用Binding语法CommandParameter="{Binding SelectedItem, ElementName=集合控件}"

                <HyperlinkButton x:Name="View1" Grid.Row="1" Content="Go to View1" Command="{Binding GotoView1}" CommandParameter="Hello" ></HyperlinkButton>

     

  • 相关阅读:
    CSS改变插入光标颜色caret-color简介及其它变色方法(转)
    Javascript常见性能优化
    安装和使用Karma-Jasmine进行自动化测试
    宝塔 ftp 不能连接 列出时出错
    windows 开机总动运行bat文件
    PHP回调函数--call_user_func_array
    php 获取数组第一个key 第一个键值对 等等
    ssi的使用 开启 配置等
    go get请求 json字符串转为结构体
    php protected 类型如何获取
  • 原文地址:https://www.cnblogs.com/zjoch/p/2653866.html
Copyright © 2020-2023  润新知