How do I Implement an interface in IronRuby that includes CLR Events

随声附和 提交于 2019-12-21 20:35:06

问题


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

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

Edit: Thanks to Kevin's response

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

回答1:


It looks like this was implemented by Tomas somewhat recently:

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

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


来源:https://stackoverflow.com/questions/1319487/how-do-i-implement-an-interface-in-ironruby-that-includes-clr-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!