I have a situation where I need to chain methods to a delegate D using the anonymous method syntax ( D += delegate(...){} ). However I need to make sure that other chained methods are removed before adding a new one. I know I can do something like D -= a, D += b, but since I'm using an anonymous method, I'm not sure whether the -= will work (since I don't have an explicit name.).
我有一种情况,我需要使用匿名方法语法( D += delegate(...){} )将方法链接到委托D。 不过,我需要确保在添加新链接之前删除其他链接方法。 我知道我可以做类似D - = a,D + = b的东西,但由于我使用的是匿名方法,因此我不确定-=是否可以工作(因为我没有明确的名称)。 。 如果我的推理是正确的,有没有办法使用匿名语法删除所有链接的方法? 提前致谢。 我不得不撤回我的原始答案,因为它是错误的,并且评论(在
I'm having some trouble with figuring out a way of unsubscribing from some anonymous delegate events that i found in a pre-made helper file that helps allow movement of controls at run time. The reason i want to unsubscribe to these events is so that the control (in this case buttons) will become locked again and not able to be moved. Here is the method in the helper class: public static
我在解决某些匿名委托事件方面遇到了一些麻烦,这些事件在预先制作的帮助文件中发现,有助于在运行时移动控件。 我想取消订阅这些事件的原因是,控件(在本例中为按钮)将再次被锁定,无法移动。 以下是助手类中的方法: public static void Init(Control control) { Init(control, Direction.Any); } public static void Init(Control control, Direction direction) { Init(control, cont
I've been reading this MSDN article and this question to try to understand events in .NET. Unfortunately, its not clicking for me and I'm having a lot of trouble. I'm trying to integrate this technique into my project, with little success. Basically, I've got this class that will read numbers. Whenever it encounters a new number, I want it to fire an event called numberChange
我一直在阅读这篇MSDN文章和这个问题,试图理解.NET中的事件。 不幸的是,它没有点击我,我有很多麻烦。 我试图将这种技术整合到我的项目中,但很少成功。 基本上,我有这个班将读取数字。 每当遇到新号码时,我都希望它发起一个名为numberChanged的事件。 所以,我设置了我的事件public event EventHandler numberChanged; 。 稍后,当我遇到一个数字时,我会触发我的事件,而不是与之前的事件不同。 if(currentNumber
After discovering lambda expressions, and their use as anonymous functions, I've found myself writing a lot of more trivial events such as these: txtLogin.GotFocus += (o, e) => { txtLogin.Text = string.Empty; txtLogin.ForeColor = SystemColors.ControlText; }; txtLogin.LostFocus += (o, e) => { txtLogin.Text = "Login..."; txtLogin.ForeColor = SystemColors.InactiveCaptionTe
在发现lambda表达式以及它们作为匿名函数的使用后,我发现自己写了很多更琐碎的事件,例如: txtLogin.GotFocus += (o, e) => { txtLogin.Text = string.Empty; txtLogin.ForeColor = SystemColors.ControlText; }; txtLogin.LostFocus += (o, e) => { txtLogin.Text = "Login..."; txtLogin.ForeColor = SystemColors.InactiveCaptionText; }; 我也摆脱了事件处理程序,它们只是调用其他函数,用相同的
This question already has an answer here: Unsubscribe anonymous method in C# 11 answers 你可以将lamdba提取到一个变量中: EventHandler func = (sender, e) => listView_PreviewTextInput(sender, e, listView); if (((bool)e.NewValue)) { listView.PreviewTextInput += func; } else { listView.PreviewTextInput -= func; } Warning! Accepted answer from Steven is wrong , all it does is just
这个问题在这里已经有了答案: 在C#11中取消订阅匿名方法的答案 你可以将lamdba提取到一个变量中: EventHandler func = (sender, e) => listView_PreviewTextInput(sender, e, listView); if (((bool)e.NewValue)) { listView.PreviewTextInput += func; } else { listView.PreviewTextInput -= func; } 警告! 从Steven接受的答案是错误的 ,它所做的只是掩盖了resharper警告的问题。 每次给定的代码
This question already has an answer here: Unsubscribe anonymous method in C# 11 answers How to remove a lambda event handler [duplicate] 1 answer If you want to use multiple anonymous event handlers and still be able to detach them later, you can simply store detach actions in a separate list, similar to: // these actions should be invoked during cleanup private readonly List<Action>
这个问题在这里已经有了答案: 在C#11中取消订阅匿名方法的答案 如何删除一个lambda事件处理程序[复制] 1个回答 如果您想使用多个匿名事件处理程序,并且以后仍可以将其分离,则可以将分离操作简单地存储在单独的列表中,如下所示: // these actions should be invoked during cleanup private readonly List<Action> _cleanupActions = new List<Action>(); public void Start() { foreach (var m in
Lets say I have an observableCollection of classes: CustomClassName testClass = new CustomClassName(); ObservableCollection<CustomClassName> collection = new ObservableCollection<CustomClassName>(); testClass.SomeEvent += OnSomeEvent; collection.add(testClass); When I will remove items from a collection, do i need manually unsubscribe from events(OnSomeEvent) or should I leave it fo
可以说我有一个observableCollection类: CustomClassName testClass = new CustomClassName(); ObservableCollection<CustomClassName> collection = new ObservableCollection<CustomClassName>(); testClass.SomeEvent += OnSomeEvent; collection.add(testClass); 当我从集合中删除项目时,是否需要手动取消订阅事件(OnSomeEvent),还是应该将它留给GC? 什么是退订的最佳方式? 如果您希望收集物品,那
I have quickly read over the Microsoft Lambda Expression documentation. This kind of example has helped me to understand better, though: delegate int del(int i); del myDelegate = x => x * x; int j = myDelegate(5); //j = 25 Still, I don't understand why it's such an innovation. It's just a method that dies when the "method variable" ends, right? Why should I use this
我已经快速阅读了Microsoft Lambda Expression文档。 虽然这样的例子帮助我更好地理解: delegate int del(int i); del myDelegate = x => x * x; int j = myDelegate(5); //j = 25 不过,我不明白为什么这是一种创新。 这只是一个方法,当“方法变量”结束时就会死掉,对吗? 为什么我应该使用这个而不是真正的方法? Lambda表达式对于匿名代理来说是一种更简单的语法,可以在任何地方使用匿名代理。 然而,事实恰恰
In a code review, I stumbled over this (simplified) code fragment to unregister an event handler: Fire -= new MyDelegate(OnFire); I thought that this does not unregister the event handler because it creates a new delegate which had never been registered before. But searching MSDN I found several code samples which use this idiom. So I started an experiment: internal class Program { pub
在代码审查中,我偶然发现了这个(简化的)代码片段来取消注册一个事件处理程序: Fire -= new MyDelegate(OnFire); 我认为这不会取消注册事件处理程序,因为它会创建一个以前从未注册过的新代理程序。 但是搜索MSDN我发现了几个使用这个习惯用法的代码示例。 所以我开始了一个实验: internal class Program { public delegate void MyDelegate(string msg); public static event MyDelegate Fire; private
All of the Func delegates return a value. What are the .NET delegates that can be used with methods that return void? All Func delegates return something; all the Action delegates return void. Func<TResult> takes no arguments and returns TResult: public delegate TResult Func<TResult>() Action<T> takes one argument and does not return a value: public delegate void Action
所有的Func代表都会返回一个值。 什么是.NET代表可以用于返回void的方法? 所有的Func代表都会返回一些东西; 所有的Action代表都返回void。 Func<TResult>接受任何参数并返回TResult: public delegate TResult Func<TResult>() Action<T>接受一个参数并且不返回值: public delegate void Action<T>(T obj) Action是最简单的'裸'代表: public delegate void Action() 还有Func<