中为按钮单击事件制作

中为按钮单击事件制作

本文介绍了如何在 WPF 4.5 中为按钮单击事件制作 MarkupExtenstion?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建 MarkupExtension 以提供对按钮单击事件的支持.我有一篇关于 PreviewKeyDown 的文章 此处.我试图为 Click 修改它但无法这样做,因为它引发了异常

I want to create the MarkupExtension to provide support for Button click event. I have got an article for PreviewKeyDown here. I tried to modify it for Click but unable to do so as it throws an exception

请建议如何实施.

推荐答案

看起来它试图将 MethodInfo 转换为 EventInfo,返回的目标属性可能是一个 MethodInfoEventInfo 您必须在创建委托之前检查这一点

Looks like its trying to cast MethodInfo as EventInfo, the returned target property could be a MethodInfo or EventInfo you will have to check this before createing the delegate

试试这个

    public class Call : MarkupExtension
    {
        public string ActionName { get; set; }
        public Call(string actionName) { ActionName = actionName; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget targetProvider = serviceProvider
                .GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            if (targetProvider == null)
                throw new InvalidOperationException(@"The CallAction extension
                can't retrieved the IProvideValueTarget service.");

            var target = targetProvider.TargetObject as FrameworkElement;
            if (target == null)
                throw new InvalidOperationException(@"The CallAction extension
                    can only be used on a FrameworkElement.");

            if (targetProvider.TargetProperty is MethodInfo)
            {
                var targetEventAddMethod = targetProvider.TargetProperty as MethodInfo;
                if (targetEventAddMethod != null)
                {
                    ParameterInfo[] pars = targetEventAddMethod.GetParameters();
                    Type delegateType = pars[1].ParameterType;
                    MethodInfo methodInfo = this.GetType().GetMethod("MyProxyHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                    return Delegate.CreateDelegate(delegateType, this, methodInfo); ;
                }

            }
            else if (targetProvider.TargetProperty is EventInfo)
            {
                var targetEventInfo = targetProvider.TargetProperty as EventInfo;
                if (targetEventInfo != null)
                {
                    Type delegateType = targetEventInfo.EventHandlerType;
                    MethodInfo methodInfo = this.GetType().GetMethod("MyProxyHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                    return Delegate.CreateDelegate(delegateType, this, methodInfo);
                }
            }
            return null;
        }

        void MyProxyHandler(object sender, EventArgs e)
        {
            FrameworkElement target = sender as FrameworkElement;
            if (target == null) return;
            var dataContext = target.DataContext;
            if (dataContext == null) return;

            //get the method on the datacontext from its name
            MethodInfo methodInfo = dataContext.GetType()
                .GetMethod(ActionName, BindingFlags.Public | BindingFlags.Instance);
            methodInfo.Invoke(dataContext, null);
        }
    }

这篇关于如何在 WPF 4.5 中为按钮单击事件制作 MarkupExtenstion?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:20