本文介绍了为 UIBarButtonItem 添加/删除 EventHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在构造函数中定义一个EventHandler:

One can define an EventHandler in the constructor:

UIBarButtonItem logoutButton = new UIBarButtonItem (UIBarButtonSystemItem.Stop, logoutButtonEventHandler);

private void logoutButtonEventHandler(object sender, EventArgs args){
    Console.WriteLine("Logout");
}

之后是否可以删除EventHandler?也许根本不使用 EventHandler 而是使用 ?我没有找到任何例子.始终只使用匿名方法.

Is it possible to remove the EventHandler afterwards? Perhaps by not using an EventHandler at all and instead use the Action/Target properties of UIBarButtonItem? I don't find any examples. Only anonymous methods are used all the time.

你是怎么做到的?

推荐答案

实例化你的对象,然后设置处理程序:

Instantiate your object and then set the handler:

var logoutButton = new UIBarButtonItem (UIBarButtonSystemItem.Stop)
logoutButton.Clicked += logoutButtonEventHandler;

要在之后删除它,请使用 -= 语法:

To remove it afterwards use the -= syntax:

 logoutButton.Clicked -= logoutButtonEventHandler;

请注意常见的陷阱 当你这样做是因为它们可能会导致内存泄漏.

Just beware of commom pitfalls when you do so because they may cause memory leaks.

这篇关于为 UIBarButtonItem 添加/删除 EventHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 13:58