Tag: events

分配给事件的lambda会阻止拥有对象的垃圾收集吗?

假设您有一个带有事件属性的类。 如果在本地上下文中实例化此类,而没有外部引用,则会为事件分配lambda表达式,以防止实例被垃圾回收? { var o = new MyClass(); o.MyClassEvent += (args) => {}; } // Will ‘o’ be eligible for garbage collection here?

请解释Timer事件async / await语法

我在这里和这里研究了asynch和await语法。 它真的有助于理解用法,但我在MSDN上找到了一个有趣的语法示例,我只是不明白。 问 :有人可以向我解释一下这个System.Timers.Timer事件注册的语法与asynch await:为什么你可以使用lambda表达式中已经存在的async await关键字? Timer timer = new Timer(1000); timer.Elapsed += async ( sender, e ) => await HandleTimer(); private Task HandleTimer() { Console.WriteLine(“\nHandler not implemented…” ); } 问题2:如果sender没有出现在HandleTimer方法中,那么sender和e的两个参数是什么?

在C#中使用Lambda的UnHooking事件#

我经常遇到想要订阅事件的情况,但我想使用lambda这样做: public class Observable { public event EventHandler SomethingHappened; public void DoSomething() { // Do Something… OnSomethingHappened(); } } // Somewhere else, I hook the event observable.SomethingHappened += (sender, args) => Console.WriteLine(“Something Happened”); 我遇到的问题是我不知道如何取消事件。 由于lambda在引擎盖下创建了一个匿名委托,我没有什么可以调用的-= on。 现在,我可以创建一个方法: private void SomethingHappened(object sender, EventArgs args) { Console.WriteLine(“Something Happened”); } 然后我可以挂钩/取消所有我想要的东西: observable.SomethingHappened += SomethingHappened; observable.SomethingHappened -= SomethingHappened; 但我真的非常非常喜欢使用我的lambda。 […]

所有TextBox的一个事件

我在WPF C#中做了一个简单的程序,我有很多TextBox – 每个TextBox做同样的事情,我很懒,为每个TextBox编写每个事件。 那么,有没有办法如何通过一个事件服务所有TextBox ? 有一个简短的代码: private void OnMouseLeft(object sender, MouseButtonEventArgs e) { TextBox1.Text = string.Empty; TextBox1.Foreground = Brushes.Black; } private void OnMouseLeft1(object sender, MouseButtonEventArgs e) { TextBox2.Text = string.Empty; TextBox2.Foreground = Brushes.Black; } 谢谢! 🙂

窗口加载和WPF

我在Windows 2012中有一个WPF项目,我需要在Window Loaded事件中加载一些信息。 不过,我需要在View Model中而不是在CodeBehind中执行此操作。 我试图使用以下代码: 在我的xaml中: 在我的视图模型中: private DelegateCommand _WindowLoadedCommand; public DelegateCommand WindowLoadedCommand { get { return _WindowLoadedCommand; } private set { _WindowLoadedCommand = value; } } public ShellViewModel() { WindowLoadedCommand = new DelegateCommand(WindowLoadedAction); } protected void WindowLoadedAction() { … } 我附加的行为: public class WindowLoadedBehavior : Behavior { [SuppressMessage(“Microsoft.StyleCop.CSharp.MaintainabilityRules”, “SA1401:FieldsMustBePrivate”, Justification = “Dependency Property. […]

以类型安全的方式处理PropertyChanged

有很多文章关于如何使用reflection和LINQ以类型安全的方式引发PropertyChanged事件,而不使用字符串。 但有没有办法以类型安全的方式使用 PropertyChanged事件? 目前,我正在这样做 void model_PropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case “Property1”: … case “Property2”: … …. } } 有没有办法避免在switch语句中硬编码字符串来处理不同的属性? 一些类似LINQ或基于reflection的方法?

是否有一种使用Unity的事件的好方法?

programAction = UnityContainer.Resolve(); (programAction as LoaderDriver).LoadComplete += new EventHandler(Program_LoadComplete); 是否有一个配置让我解决我已连接到事件的对象? 或者,是否有一种获得相同结果的首选方法? 我注意到有时当我没有看到“特征”时,因为我不知道的模式是首选。

使用UWP TextBox.TextChanging来忽略错误的数据

我正在创建一个UWP应用程序,它具有不同的TextBoxes来输入数字。 为确保只能输入数字,我使用TextChanging事件。 遗憾的是,我找不到任何关于如何使用TextChanging来忽略错误输入的文档。 一个TextBox的工作解决方案如下: string oldText; private void tbInput_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) { double temp; if (double.TryParse(sender.Text, out temp) || sender.Text == “”) oldText = sender.Text; else { int pos = sender.SelectionStart – 1; sender.Text = oldText; sender.SelectionStart = pos; } } 使用这个解决方案,我需要为每个TextBox提供一个string oldText ,并为每个TextBox提供一个TextChanging函数或者在函数内部有更多代码。 是否有一种简单的方法可以忽略TextBox.TextChanging事件中的“错误”输入?

使用C ++将托管事件公开给COM

可以公开在C#中编写的托管事件,以便在使用c ++编写的COM对象中公开和使用。 不熟悉com和atl。 您能否展示一下MSDN文章中显示的C ++方面的内容 http://msdn.microsoft.com/en-us/library/dd8bf0x3.aspx 显示的VB6代码certificate它是可行的。

C#知道设置了多少个EventHandler?

众所周知,我们可以创建一个EventHandler并向其添加N次方法。 喜欢: // Declare and EventHandler public event EventHandler InternetConnectionAvailableEvent; private void OnInternetConnectionAvailableEvent() { if (InternetConnectionAvailableEvent != null) { EventHandler handle = InternetConnectionAvailableEvent; EventArgs e = EventArgs.Empty; handle(this, e); } } // IN OTHER CLASS WHERE I USE THE EVENT // Set the method name to handle the event monitorInternet.InternetConnectionAvailableEvent += HandleInternetConnectionEvent; void HandleInternetConnectionEvent(object sender, […]