需要知道如何创建DelegateCommand;返回一个整数值

发布于 2024-09-04 15:39:48 字数 1041 浏览 2 评论 0原文

这就是我正在尝试做的事情。请密切注意 InsertConlogEntry 以及我如何尝试将其分配给我的 DelegateCommand,但是,我如何处理返回 int?

谢谢你,

   #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);
    }

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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-09-11 15:39:48

命令是动作,而不是功能。因此,它们不应该返回任何值。

命令是响应某些输入而触发的操作。从技术上讲,逻辑应该是独立的。

话虽这么说,如果您只需要调用此方法并忽略结果,您可以将其包装在 Action 中,并使用该方法构建委托命令:

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

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); 
挽心 2024-09-11 15:39:48

你不能。它只需要一个Action

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

你げ笑在眉眼 2024-09-11 15:39:48

必须定义您自己的 DelegateCommand 版本,该版本接受 Func 作为构造函数参数,而不是简单的 Action< /代码>。

由于命令本身并不关心返回值,因此您可以像这样初始化它:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文