zoukankan      html  css  js  c++  java
  • Silverlight Command的运用

    1:写一个类继承ICommand接口

    public class MyCommand : ICommand
        {
    
            private readonly Action<object> executeAction;
            private readonly Func<object, bool> canExecuteAction;
            public event EventHandler CanExecuteChanged;
            public MyCommand(Action<object> executeAction)
                : this(executeAction, null)
            {
            }
            public MyCommand(Action<object> executeAction,
                Func<object, bool> canExecuteAction)
            {
                this.executeAction = executeAction;
                this.canExecuteAction = canExecuteAction;
            }
            public bool CanExecute(object parameter)
            {
                if (canExecuteAction != null)
                {
                    return canExecuteAction((object)parameter);
                }
                return true;
            }
            public void Execute(object parameter)
            {
                if (CanExecute(parameter))
                {
                    executeAction((object)parameter);
                }
            }
    
        }

    2:在ViewMode中定义一个Comand 返回一个自定义Command实例,传入方法

      public class MainPageViewMode
        {
            public ICommand ShowHello { get { return new MyCommand(showhello); } }
    
    
            private void showhello(object param)
            {
                MessageBox.Show("Hello!");
            }
    
        }

    3:前台运用

     <Button x:Name="addxap" Content="CommandTest" Command="{Binding ShowHello}" />
    

    这样,点击按钮的时候就会执行ViewMode中的方法了。

  • 相关阅读:
    MySQL数据库 : 高级查询
    ElasticSearch : High Rest Api 使用
    Java : JavaWeb和Tomcat相关
    Linux : Ubuntu 安装 RabbitMQ
    Spring : Spring Security
    Java : logback简单配置
    Spring : JPA的单独使用
    Java : Netty 入门案例
    python__PIP : 安装第三方库
    好文章收藏(持续更新)
  • 原文地址:https://www.cnblogs.com/Rmeo/p/3089446.html
Copyright © 2011-2022 走看看