How to implement an interface in IronRuby that includes CLR events

I am experimenting with IronRuby and WPF, and I would like to write my own teams. What I have below, as far as I can understand.

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

But the ICommand interface defines the CanExecuteChanged event. How to implement this in IronRuby?

Edit: thanks to Kevin's answer

Here's what works based on the 27223 DLR change set. The value passed to can_execute and execute is 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
+3
source share
1 answer

It seems that this was implemented by Tomas a few recently :

So you may need to compile from the last source in github

, . , add_ remove_ . - (, , , ):

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

Source: https://habr.com/ru/post/1715838/


All Articles