2 c512026052 c512026052 于 2015.05.29 15:03 提问

MVVM模式如何将方法作为参数传递且同时指定回调函数?

MyViewModel是我能修改的类,AlertMessage类是人家封装好的类,我不能修改。请问我要怎么才能实现如题所述的?以下是我的代码

public class MyViewModel
    {
        public MyViewModel()
        {
            SelectCommand = new DelegateCommand(SelectCommand_Execute); ;
        }       

        public ICommand SelectCommand { get; set; }

        protected virtual void SelectCommand_Execute()
        {
        }

        // 我要怎么传递这个回调函数,这个回调函数没有CommandExecutingMessage
        private bool Callback(ICommand myCommand)
        {
            return true;
        }

        public void DoSomething()
        {
            MessageBus bus = new MessageBus();

            // Questions 
            // WithCommandLifetime方法把一个方法作为参数,同时指定了一个回调            
            // 注意WithCommandLifetime()方法检查了CommandExecutingMessage类型
            // 我要怎么样才能获取到CommandExecutingMessage类型来使WithCommandLifetime()指定回调函数
            // 我要如何构建这个调用?
            bus.Warning("This is test message").WithCommandLifetime(what goes here);
        }
    }
 public class AlertMessage
    {
        public AlertMessage(string msg)
        {
            this.Message = msg;
        }

        public string Message { get; set; }

        public bool IsExpired(object message)
        {
            var result = false;

            if (IsExpiredDelegate != null)
            {
                result = IsExpiredDelegate(message);
            }

            return result;
        }

        public Func<object, bool> IsExpiredDelegate
        {
            get;
            set;
        }
    }

    public class MessageBus
    {
        public AlertMessage Warning(string msg)
        {
            return new AlertMessage(msg);
        }
    }

    public static class AlertMessageLifetimePolicies
    {
        public static AlertMessage WithCommandLifetime(this AlertMessage source, Func<ICommand, bool> isExpiredCallback)
        {
            source.IsExpiredDelegate = data =>
            {
                var result = false;
                var asCommandExecutingMessage = (data as CommandExecutingMessage);
                if (asCommandExecutingMessage != null)
                {
                    result = isExpiredCallback(asCommandExecutingMessage.Sender);
                }

                return result;
            };

            return source;
        }
    }

    public class CommandExecutingMessage
    {
        private ICommand _sender;
        public CommandExecutingMessage(ICommand sender)
        {
            this._sender = sender;
        }

        new public ICommand Sender
        {
            get { return this._sender; }
        }
    }

1个回答

c512026052
c512026052   2015.05.29 17:07
已采纳

找到答案了,调用方法如下:

  public void DoSomething()
        {
            MessageBus bus = new MessageBus();
            //assign callback method
            AlertMessage alertMessage = bus.Warning("this").WithCommandLifetime((x) => Callback(SelectCommand));
            CommandExecutingMessage commandExcutingMessage = new CommandExecutingMessage(SelectCommand);
            //call the Callback method
            alertMessage.IsExpiredDelegate(commandExcutingMessage);
        }
Csdn user default icon
上传中...
上传图片
插入图片