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

发布于 2024-08-02 11:01:06 字数 1028 浏览 5 评论 0 原文

我正在尝试使用 IronRuby 和 WPF,我想编写自己的 命令。据我所知,下面是我能弄清楚的。

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

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

编辑:感谢 Kevin 的回复

以下是基于 DLR 27223 更改集的工作原理。传入can_execute 和execute 的值为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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2024-08-09 11:01:06

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

所以你可能需要从 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文