开发者

Need to know how to create a DelegateCommand<Type> that returns an integer value

Here is what I am trying to do. Pay close attention to the InsertConlogEntry and how I am trying to assign it to my DelegateCommand, but, how do I handle the return int?

Thank you,

   #region Members        
    public static readonly string AddConlogEntry = "AddConlogEntry";
    public static readonly string GetConlogList = "GetConlogList";
    public static readonly string GetConlogEntry = "GetConlogEntry";            
    public static readonly string Scope = "ConLog";
    #endregion

    #region Ctor
    public ConLogCommands() :base()
    {
        scope = Scope;

        DelegateCommand<ConlogEntryData> AddEntryCmd = new DelegateCommand<ConlogEntryData>("Add New Conlog Entry",
            "AddConLogEntry", InsertConlogEntry);            
        this.Add(AddEntryCmd);
    }
    #endregion

    #region Methods
    private int InsertConlogEntry(ConlogEntryData data)
    {
        ConlogService service = new ConlogService();
        return service.InsertConlogEntry(data.LoanNumber, data.UserId, data.Subject, data.Comment, data.EntryDate);
  开发者_JS百科  }


Commands are meant to be actions - not functions. As such, they shouldn't return any values.

A command is meant to be an action that fires in response to some input. The logic should, technically, be self-contained.

That being said, if you need to just call this method and ignore the results, you can wrap it in an Action<ConlogEntryData> and build your delegate command with that:

 Action<ConlogEntryData> action = c => this.InsertConlogEntry(c);
 DelegateCommand<ConlogEntryData> AddEntryCmd = new DelegateCommand<ConlogEntryData>(
         "Add New Conlog Entry","AddConLogEntry", action); 


You can't. It only takes an Action<T>.


Not without defining your own version of DelegateCommand<T> that accepts a Func<T, int> as a constructor parameter rather than a simple Action<T>.

Since the command itself doesn't care about the return value, you can initialize it like this:

AddEntryCmd = new DelegateCommand<ConlogEntryData>("Add New Conlog Entry",
    "AddConLogEntry", e => InsertConlogEntry(e));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜