Tag: 事件

将事件从自定义控件传播到表单

我有一个自定义控件,如: public sealed class BorderEx : Control { public static readonly RoutedEvent ReloadClickEvent = EventManager.RegisterRoutedEvent(“ReloadClick”, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BorderEx)); public event RoutedEventHandler ReloadClick { add { AddHandler(ReloadClickEvent, value); } remove { RemoveHandler(ReloadClickEvent, value); } } void RaiseReloadClickEvent() { var newEventArgs = new RoutedEventArgs(ReloadClickEvent); RaiseEvent(newEventArgs); } static BorderEx() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderEx), new FrameworkPropertyMetadata(typeof(BorderEx))); } } 并且在reloadButton上发生了事件,点击generic.xaml </ControlTemplate 但我不知道如何从内部按钮引发此外部事件。 […]

阻止代码并等待事件处理程序触发?

我正在编写一个使用第三方库来处理某些数据的应用程序。 在一个非常简单的例子中,我有一个像这样的任务运行的方法: private void ProcessListOfItems(List items) { while (items.Count > 0) { 3rdPartyLibObject.Process(items[0]); items.Remove(0); } } 正如您所看到的,我的代码当前编写的方式,我会在Process()方法返回后立即从列表中删除每个项目。 但是项目的处理可能会失败,我需要知道是否会发生这种情况。 不幸的是,Process()方法不返回bool值来指示项目是否已成功处理,而是会触发ProcessingComplete和ProcessingFailed事件。 我有事件处理程序连接到这样的事件: 3rdPartyLibObject.ProcessingComplete += obj_ProcessingSuccess; 3rdPartyLibObject.ProcessingFailed += obj_ProcessingFailed; private void obj_ProcessingSuccess(object sender, 3rdPartyLibObject.ProcessingEventArgs e) { this.Invoke(new ProcessedHandler(OnProcessed), new object[] { true }); } private void obj_ProcessingFailed(object sender, 3rdPartyLibObject.ProcessingEventArgs e) { this.Invoke(new ProcessedHandler(OnProcessed), new object[] { false }); […]

在代码中添加表单关闭事件到特定对象实例?

非常简单我想做什么,只是希望能够在表单关闭后运行一些代码。 Form1 f = new Form1(); f.Show(); f.formClosing … <- I just want to run code from this context once this form has been closed

C#事件inheritance

我有这个程序: class One { public delegate void del(object o); public event del SomethingChanged; int x; public int X { get { return x; } set { x = value; OnSomethingChanged(this); } } protected void OnSomethingChanged(object o) { if (SomethingChanged != null) SomethingChanged(o); } } class Two: One { public void ChangeSomething() { //if (SomethingChanged != […]

如何引发跨线程事件

如何从另一个线程引发事件GeocodeAddressEventHandler? System.Threading.Thread MapThread; WinformMap map ; public delegate void GeocodeAddressEventHandler(object sender, EventArgs e); public event GeocodeAddressEventHandler GeocodeAddressEvent; //How to Raise this Event from another thread?? public void GeocodeAddress_Raised(object sender, EventArgs e) { MapLib.GeocodeAddress(“12798 1ST ST”, “”, “”, “”); } public bool LoadMap(string restorepoint) { ////////////////////////Engine Controls//////////////////////////////////////////////////////// try { System.ServiceModel.OperationContext context = System.ServiceModel.OperationContext.Current; //This is to […]

c#:无法访问父类的事件?

从来没见过这个。 这是样本: using System; namespace Testing { public class Test { public event Action ActionRequired; } public class ChildTest : Test { public void DoSomething() { if (this.ActionRequired != null) this.ActionRequired(); } } } 这不起作用,错误是我只能从基类访问该事件。 到处运行并不困难(向基类添加一个受保护的方法,它既可以检查事件的调用,也可以从子类中调用该方法),但我真的很想知道这个限制背后的想法是什么? 干杯, SEBI

设计帮助 – 多态事件处理

设计问题 – 多态事件处理 我目前正在尝试减少当前项目中的事件句柄数量。 我们有多个通过USB发送数据的系统。 我目前有一个例程来读取消息并解析初始标头详细信息以确定消息来自哪个系统。 标题有点不同,所以我创建的EventArgs不一样。 然后我通知所有“观察员”这一变化。 所以我现在所拥有的是以下内容: public enum Sub1Enums : byte { ID1 = 0x01, ID2 = 0x02 } public enum Sub2Enums : ushort { ID1 = 0xFFFE, ID2 = 0xFFFF } public class MyEvent1Args { public Sub1Enums MessageID; public byte[] Data; public MyEvent1Args(Sub1Enums sub1Enum, byte[] data) { MessageID = sub1Enum; Data […]

IPostbackEventHandler VS IPostbackDataHandler

1)用户在DropDownList中选择项目被认为是回发数据,因此DropDownList实现IPostbackDataHandler 。 a)但是为什么用户移动(在Calendar控件中)到另一个月也没有被认为是回发数据? 那么,为什么Calendar实现IPostbackEventHandler而不是IPostbackDataHandler呢? 2) a)我假设实现IPostbackEventHandler而不是IPostbackDataHandler的控件永远不会收到回发数据? b)如果控件实现了IPostbackDataHandler ,那么每次数据更改时都会触发控件的回发事件,即使该控件没有引发回发 但是如果控件实现了IPostbackEventHandler ,那么只有控件的回发事件会被引发的时间是该控件是否也触发了回发?

在用户控件中解释自定义事件的代码

有人给了我这个效果很好的代码。 但我真的很想了解其中发生的事情。 有人可以解释一下吗? 代码的每个部分的含义是什么? 代码位于自定义控件内,该控件在面板内有两个标签。 此外,我已经看到一些使用添加/删除语法的自定义控件事件,这是为了什么? 与这里发生的事情有什么不同? public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public event EventHandler MyCustomClickEvent; protected virtual void OnMyCustomClickEvent(EventArgs e) { // Here, you use the “this” so it’s your own control. You can also // customize the EventArgs to pass something you’d like. if (MyCustomClickEvent != […]

EventHandler为null

我试图从用户控件中引发单击事件并在包含页面上处理它。 我遇到的问题是,当我点击用户控件上的’imgstep1’按钮时,imgstep1_click事件后面的代码会触发,但’btnHandler’事件总是为空。 因此它不会调用父事件。 任何有关这方面的帮助将不胜感激。 我的用户控制代码是: .ascx代码: .ascx.cs代码: public delegate void OnImageButtonClick(); public event OnImageButtonClick btnHandler; protected void imgstep1_Click(object sender, ImageClickEventArgs e) { if (btnHandler != null) btnHandler(); } .aspx页面代码: protected void Page_Load(object sender, EventArgs e) { ucStepHdr.btnHandler += new StepsHeader.OnImageButtonClick(ucStepHdr_btnHandler); } void ucStepHdr_btnHandler() { Response.Write (‘test’); }