程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> mvvm模式-MVVM模式如何將方法作為參數傳遞且同時指定回調函數?

mvvm模式-MVVM模式如何將方法作為參數傳遞且同時指定回調函數?

編輯:編程綜合問答
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; }
        }
    }

最佳回答:


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

  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);
        }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved