IronRuby 和处理 XAML UI 事件

发布于 2024-08-13 18:41:58 字数 480 浏览 2 评论 0原文

通过 IronRuby 脚本向 XAML 中的 UI 元素添加事件处理程序的最简单明了的方法是什么?假设:添加事件处理程序的代码将编写在 IronRuby 脚本中,处理事件的代码将编写在同一个 IronRuby 脚本中。

我想要与以下代码等效的代码,但在 IronRuby 中。处理一个简单的button1点击事件。

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        button1.Click += new RoutedEventHandler(button1_Click);
    }

    void button1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Hello World!");
    }
}

What it is the most brief and concise way of adding event handlers to UI elements in XAML via an IronRuby script? Assumption: the code to add the event handler would be written in the IronRuby script and the code to handle the event would be in the same IronRuby script.

I'd like the equivalent of the following code but in IronRuby. Handling a simple button1 click event.

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        button1.Click += new RoutedEventHandler(button1_Click);
    }

    void button1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Hello World!");
    }
}

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

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

发布评论

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

评论(2

十年九夏 2024-08-20 18:41:58

据我所知,.NET 事件在 IronRuby 中作为采用块的方法公开。因此,您可以编写:

button1.click do |sender, args|
    MessageBox.show("Hello World!")
end

涵盖了其他选项。

So far as I know, .NET events are exposed as methods taking blocks in IronRuby. So, you can write:

button1.click do |sender, args|
    MessageBox.show("Hello World!")
end

This covers other options.

橙幽之幻 2024-08-20 18:41:58

如果您觉得更有意义,您还可以使用 add 来订阅事件。

p = Proc.new do |sender, args| 
  MessageBox.show("Hello  World!")
end

# Subscribe
button1.click.add p

# Unsubscribe
button1.click.remove p

You can also use add to subscribe an event if it makes more sense to you.

p = Proc.new do |sender, args| 
  MessageBox.show("Hello  World!")
end

# Subscribe
button1.click.add p

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