首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在IronRuby中实现包含CLR事件的接口

如何在IronRuby中实现包含CLR事件的接口
EN

Stack Overflow用户
提问于 2009-08-23 20:35:41
回答 1查看 443关注 0票数 3

我正在试验IronRuby和WPF,我想编写自己的命令。下面是我所能找到的。

代码语言:javascript
复制
class MyCommand
  include System::Windows::Input::ICommand
  def can_execute()
    true
  end
  def execute()
    puts "I'm being commanded"
  end
end

但是ICommand接口定义了CanExecuteChanged事件。我如何在IronRuby中实现这一点?

编辑:感谢凯文的回应

下面是基于DLR 27223变更集的工作原理。传入can_execute和execute的值为零。

代码语言:javascript
复制
class MyCommand
  include System::Windows::Input::ICommand
  def add_CanExecuteChagned(h)
    @change_handlers << h
  end
  def remove_CanExecuteChanged(h)
    @change_handlers.remove(h)
  end
  def can_execute(arg)
     @can_execute
  end
  def execute(arg)
    puts "I'm being commanded!"
    @can_execute = false
    @change_handlers.each { |h| h.Invoke(self, System::EventArgs.new) }
  end
  def initialize
    @change_handlers = []
    @can_execute = true
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-08-24 00:18:18

看起来这是由Tomas 有点最近实现的:

因此,您可能需要从github的最新源代码编译。

看起来,您需要添加一个处理程序传入和存储的位置。即,为所讨论的特定事件处理程序添加一些add_和remove_例程。像这样的东西可能会根据你的需求(天真,所以请测试和充实):

代码语言:javascript
复制
class MyCommand
  include System::Windows::Input::ICommand
  def add_CanExecuteChanged(h)
    @change_handler = h
  end

  def remove_CanExecuteChanged
    @change_handler = nil
  end

  def can_execute()
    true
  end

  def execute()
    #puts "I'm being commanded"
    @change_handler.Invoke if @change_handler
  end
end
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1319487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档