如何在IronRuby中实现包含CLR事件的界面 [英] How do I Implement an interface in IronRuby that includes CLR Events

查看:132
本文介绍了如何在IronRuby中实现包含CLR事件的界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试IronRuby和WPF,我想自己写一个命令。我有以下的东西,据我所知。

I'm experimenting with IronRuby and WPF and I'd like to write my own commands. What I have below Is as far as I can figure out.

class MyCommand
  include System::Windows::Input::ICommand
  def can_execute()
    true
  end
  def execute()
    puts "I'm being commanded"
  end
end

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

But the ICommand interface defines the CanExecuteChanged event. How do I implement that in IronRuby?

编辑:感谢Kevin的回应

这是基于DLR的27223更改集的工作原理。传递给can_execute和执行的值为nil。

Here's what works based on the 27223 change set of the DLR. The value passed in to can_execute and execute are nil.

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


推荐答案

看起来这是由Tomas <稍后有一个href =http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=1852 =nofollow noreferrer>

It looks like this was implemented by Tomas somewhat recently:

所以您可能需要从 github 的最新来源编译

So you may need to compile from the latest source at github

看起来您需要为要传入和存储的处理程序添加一个地方。也就是说,通过为特定的事件处理程序添加一些add_和remove_例程。
这样的东西可能会根据你的需要工作(天真的,所以请测试和完成):

It looks like you need to add a place for the handler to be passed in and stored. Namely, by adding some add_ and remove_ routines for the specific event handler in question. Something like this might work based on your needs (naive, so please test and flesh out):

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

这篇关于如何在IronRuby中实现包含CLR事件的界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆