zoukankan      html  css  js  c++  java
  • WPF 自定义事件

    1.可传参数

    namespace DrugInfo
    {
        public class ChooseDrugRoutedEventArgs : RoutedEventArgs
        {
            public ChooseDrugRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }
    
            public string Message { get; set; }
        }
        
        public partial class ChooseDrug
        {
            //声明和注册路由事件
            public static readonly RoutedEvent ChooseDrugRoutedEvent = EventManager.RegisterRoutedEvent("ChooseDrug", RoutingStrategy.Bubble, typeof(ChooseDrugRoutedEventArgs), typeof(ChooseDrug));
    
            //自定义事件委托 
            public delegate void ChooseDrugRoutedEventHandler(object sender, ChooseDrugRoutedEventArgs e);
    
            //CLR事件包装
            public event ChooseDrugRoutedEventHandler ChooseDrugClick
            {
                add { AddHandler(ChooseDrugRoutedEvent, value); }
                remove { RemoveHandler(ChooseDrugRoutedEvent, value); }
            }
            //界面选择数据后确定
            private void Choose_OnClick(object sender, RoutedEventArgs routedEventArgs)
            {           
                
                ChooseDrugRoutedEventArgs args = new ChooseDrugRoutedEventArgs(ChooseDrugRoutedEvent, this);
                args.Message = "传数据过去";
                this.RaiseEvent(args);//UIElement及其派生类 
            }
        }
    }
    
    //调用
    namespace Purchasing
    {
        /// <summary>
        /// Main.xaml 的交互逻辑
        /// </summary>
        public partial class Main
        {    
            private void Control_Loaded(object sender, RoutedEventArgs e)
            {
                var popups = new ChooseDrug();
                popups.ChooseDrugClick += Popups_ChooseDrugClick;
            }
            
            private void Popups_ChooseDrugClick(object sender, ChooseDrugRoutedEventArgs e)
            {
                RadWindow.Alert(e.Message);
            }
        }
    }

    2.无参数,不需要自定义委托

    1.可传参数
    
    namespace DrugInfo
    {
        public partial class ChooseDrug
        {
            //声明和注册路由事件
            public static readonly RoutedEvent ChooseDrugRoutedEvent = EventManager.RegisterRoutedEvent("ChooseDrug", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventHandler>), typeof(ChooseDrug));
    
            //CLR事件包装
            public event RoutedEventHandler ChooseDrugClick
            {
                add { AddHandler(ChooseDrugRoutedEvent, value); }
                remove { RemoveHandler(ChooseDrugRoutedEvent, value); }
            }
            //界面选择数据后确定
            private void Choose_OnClick(object sender, RoutedEventArgs routedEventArgs)
            {           
                RaiseEvent(new RoutedEventArgs(ChooseDrugRoutedEvent));
            }
        }
    }
    
    //调用
    namespace Purchasing
    {
        /// <summary>
        /// Main.xaml 的交互逻辑
        /// </summary>
        public partial class Main
        {    
            private void Control_Loaded(object sender, RoutedEventArgs e)
            {
                var popups = new ChooseDrug();
                popups.ChooseDrugClick += Popups_ChooseDrugClick;
            }
            
            private void Popups_ChooseDrugClick(object sender, RoutedEventArgs e)
            {
                RadWindow.Alert("OK");
            }
        }
    }
  • 相关阅读:
    Java -- 基于JDK1.8的LinkedList源码分析
    Java -- 基于JDK1.8的ArrayList源码分析
    Android -- AsyncTask源码解析
    Android -- 自定义view实现keep欢迎页倒计时效果
    Android -- 《 最美有物》好看的点赞效果
    Android -- Glide框架详解(一)
    Android -- 从源码解析Handle+Looper+MessageQueue机制
    面试 -- 关于Activity的相关知识
    用最简单的一个例子看maven冲突的解决办法
    【跟我一起读 linux 源码 01】boot
  • 原文地址:https://www.cnblogs.com/vipsoft/p/7020966.html
Copyright © 2011-2022 走看看