温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#如何定义事件

发布时间:2021-12-01 11:48:22 来源:亿速云 阅读:319 作者:小新 栏目:编程语言

小编给大家分享一下C#如何定义事件,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

C#定义事件应用

最近公司在上一个wpf项目,熟悉WPF的同学都知道,WPF控件中,"用户控件"这个概念非常常见,我们也经常要做一些用控件来实现一些相对比较复杂的功能,比如:一个二维的仓库管理系统,仓库中的货架可以做成一个用户控件,而货架中的某个货架层,货架层中的某个货格,其实都可以是一个用户控件, 我们在画具体的某个货架的时候,就可以根据这个货架的实际情况,从据库中读取相关的资料,生成具有几格几层的二维货架图形.由于货架的通过几层用户控件来实现的,有时候我们需要在它们"层次"中传递消息,比如,我的某个货格的信息变动了,需要通知整个货架,甚至是加载这个货架的某个窗口,这时候就可以C#定义事件应用来完成了,从触发事件的某一"层"起,往上抛出事件,父控件接收事件,然后接着往上抛,一直到接收这个事件的某"层"做出具体的事件处理.

首先我们做一个简单的用户控件,模拟在***层触发事件的图形控件:

<UserControlx:ClassUserControlx:Class="WpfApplication5.uc1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="60"Width="200"> <Grid> <RectangleFillRectangleFill="Bisque"></Rectangle>  </Grid> </UserControl>  usingSystem;  usingSystem.Collections.Generic;  usingSystem.Linq;  usingSystem.Text;  usingSystem.Windows;  usingSystem.Windows.Controls;  usingSystem.Windows.Data;  usingSystem.Windows.Documents;  usingSystem.Windows.Input;  usingSystem.Windows.Media;  usingSystem.Windows.Media.Imaging;  usingSystem.Windows.Navigation;  usingSystem.Windows.Shapes;   namespaceWpfApplication5  {  ///<summary> ///Interactionlogicforuc1.xaml  ///</summary> publicpartialclassuc1:UserControl  {  publicuc1()  {  InitializeComponent();  }   privatestring_name;   publicstringName  {  get;  set;  }  }  publicclassuc1ClickEventArgs  {  publicstringName  {  get;  set;  }  }  }

uc1ClickEventArgs 类是一个自定义事件参数类,用来装这个控件的一些信息,供它的上级容器调用.

再下来也是一个用户控件,用来装多个上面图形控件,比如我们可以把它看成是某个货格,而下面就是一个货架,我采用最基本的循环来生成几个上图中的用户控件:

<UserControlx:ClassUserControlx:Class="WpfApplication5.whs_map" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication5" Height="300"Width="600"Loaded="UserControl_Loaded"> <Grid> <Canvasx:NameCanvasx:Name="pa"></Canvas> </Grid> </UserControl>  Code  usingSystem;  usingSystem.Collections.Generic;  usingSystem.Linq;  usingSystem.Text;  usingSystem.Windows;  usingSystem.Windows.Controls;  usingSystem.Windows.Data;  usingSystem.Windows.Documents;  usingSystem.Windows.Input;  usingSystem.Windows.Media;  usingSystem.Windows.Media.Imaging;  usingSystem.Windows.Navigation;  usingSystem.Windows.Shapes;   namespaceWpfApplication5  {  ///<summary> ///Interactionlogicforwhs_map.xaml  ///</summary> ///   publicdelegatevoidtestDelegate(objectsender,uc1ClickEventArgse);    publicpartialclasswhs_map:UserControl  {  publicwhs_map()  {  InitializeComponent();  }   privateeventtestDelegate_testEvent;   publiceventtestDelegatetestEvent  {  add  {  _testEvent+=value;  }  remove  {  _testEvent-=value;  }  }   privatevoidUserControl_Loaded(objectsender,RoutedEventArgse)  {  intleft=5;  inttop=1;   for(inti=0;i<5;i++)  {  uc1uc=newuc1();  uc.MouseLeftButtonDown+=newMouseButtonEventHandler(mouseDown);   uc.Name=i.ToString();  pa.Children.Add(uc);   Canvas.SetTop(uc,top);  Canvas.SetLeft(uc,left);   left+=205;  }  }   publicvoidmouseDown(objectsender,MouseButtonEventArgse)  {  if(senderisuc1)  {  uc1uc=senderasuc1;   uc1ClickEventArgse2=newuc1ClickEventArgs();  e2.Name=uc.Name;  _testEvent(this,e2);  }  }  }  }

看完了这篇文章,相信你对“C#如何定义事件”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI