C#定义事件应用

C#定义事件应用

目前创新互联已为千余家的企业提供了网站建设、域名、雅安服务器托管绵阳服务器托管、企业网站设计、新蔡网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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

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

 
 
 
  1. <UserControlx:ClassUserControlx:Class="WpfApplication5.uc1" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. Height="60"Width="200"> 
  5.  
  6. <RectangleFillRectangleFill="Bisque"> 
  7.  
  8.  
  9.  
  10.  
  11. usingSystem;  
  12. usingSystem.Collections.Generic;  
  13. usingSystem.Linq;  
  14. usingSystem.Text;  
  15. usingSystem.Windows;  
  16. usingSystem.Windows.Controls;  
  17. usingSystem.Windows.Data;  
  18. usingSystem.Windows.Documents;  
  19. usingSystem.Windows.Input;  
  20. usingSystem.Windows.Media;  
  21. usingSystem.Windows.Media.Imaging;  
  22. usingSystem.Windows.Navigation;  
  23. usingSystem.Windows.Shapes;  
  24.  
  25. namespaceWpfApplication5  
  26. {  
  27. /// 
  28. ///Interactionlogicforuc1.xaml  
  29. /// 
  30. publicpartialclassuc1:UserControl  
  31. {  
  32. publicuc1()  
  33. {  
  34. InitializeComponent();  
  35. }  
  36.  
  37. privatestring_name;  
  38.  
  39. publicstringName  
  40. {  
  41. get;  
  42. set;  
  43. }  
  44. }  
  45. publicclassuc1ClickEventArgs  
  46. {  
  47. publicstringName  
  48. {  
  49. get;  
  50. set;  
  51. }  
  52. }  

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

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

 
 
 
  1. <UserControlx:ClassUserControlx:Class="WpfApplication5.whs_map" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. xmlns:local="clr-namespace:WpfApplication5" 
  5. Height="300"Width="600"Loaded="UserControl_Loaded"> 
  6.  
  7. <Canvasx:NameCanvasx:Name="pa"> 
  8.  
  9.  
  10.  
  11. Code  
  12. usingSystem;  
  13. usingSystem.Collections.Generic;  
  14. usingSystem.Linq;  
  15. usingSystem.Text;  
  16. usingSystem.Windows;  
  17. usingSystem.Windows.Controls;  
  18. usingSystem.Windows.Data;  
  19. usingSystem.Windows.Documents;  
  20. usingSystem.Windows.Input;  
  21. usingSystem.Windows.Media;  
  22. usingSystem.Windows.Media.Imaging;  
  23. usingSystem.Windows.Navigation;  
  24. usingSystem.Windows.Shapes;  
  25.  
  26. namespaceWpfApplication5  
  27. {  
  28. /// 
  29. ///Interactionlogicforwhs_map.xaml  
  30. /// 
  31. ///  
  32.  
  33. publicdelegatevoidtestDelegate(objectsender,uc1ClickEventArgse);  
  34.  
  35.  
  36. publicpartialclasswhs_map:UserControl  
  37. {  
  38. publicwhs_map()  
  39. {  
  40. InitializeComponent();  
  41. }  
  42.  
  43. privateeventtestDelegate_testEvent;  
  44.  
  45. publiceventtestDelegatetestEvent  
  46. {  
  47. add  
  48. {  
  49. _testEvent+=value;  
  50. }  
  51. remove  
  52. {  
  53. _testEvent-=value;  
  54. }  
  55. }  
  56.  
  57. privatevoidUserControl_Loaded(objectsender,RoutedEventArgse)  
  58. {  
  59. intleft=5;  
  60. inttop=1;  
  61.  
  62. for(inti=0;i<5;i++)  
  63. {  
  64. uc1uc=newuc1();  
  65. uc.MouseLeftButtonDown+=newMouseButtonEventHandler(mouseDown);  
  66.  
  67. uc.Name=i.ToString();  
  68. pa.Children.Add(uc);  
  69.  
  70. Canvas.SetTop(uc,top);  
  71. Canvas.SetLeft(uc,left);  
  72.  
  73. left+=205;  
  74. }  
  75. }  
  76.  
  77. publicvoidmouseDown(objectsender,MouseButtonEventArgse)  
  78. {  
  79. if(senderisuc1)  
  80. {  
  81. uc1uc=senderasuc1;  
  82.  
  83. uc1ClickEventArgse2=newuc1ClickEventArgs();  
  84. e2.Name=uc.Name;  
  85. _testEvent(this,e2);  
  86. }  
  87. }  
  88. }  

以上介绍C#定义事件应用

分享名称:C#定义事件应用
网页路径:http://www.hantingmc.com/qtweb/news4/535854.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联