C#/Lambda:下面的param指的是什么?

发布于 2024-09-26 10:36:21 字数 534 浏览 1 评论 0原文

我正在查看 此处 中的代码

/// <summary>
/// Returns the command that, when invoked, attempts
/// to remove this workspace from the user interface.
/// </summary>
public ICommand CloseCommand
{
    get
    {
        if (_closeCommand == null)
            _closeCommand = new RelayCommand(param => this.OnRequestClose());

        return _closeCommand;
    }
}

param >参数=> this.OnRequestClose() 指的是?

I am looking at the code from here

/// <summary>
/// Returns the command that, when invoked, attempts
/// to remove this workspace from the user interface.
/// </summary>
public ICommand CloseCommand
{
    get
    {
        if (_closeCommand == null)
            _closeCommand = new RelayCommand(param => this.OnRequestClose());

        return _closeCommand;
    }
}

what does param in param => this.OnRequestClose() refer to?

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

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

发布评论

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

评论(4

地狱即天堂 2024-10-03 10:36:21

RelayCommand 可能是接受单个参数的委托类型,或者是本身在构造函数中采用此类委托类型的类型。您正在声明一个匿名方法,简单地说“调用时,我们将获取传入值(但随后不使用它),并调用 OnRequestClose。您还可以(也许更清楚)

_closeCommand = new RelayCommand(delegate { this.OnRequestClose(); });

:在使用它的其他用途中可能更清楚,例如:

var ordered = qry.OrderBy(item => item.SomeValue);

其中 lambda 是“给定一个 item,获取 itemSomeValue”。在您的情况下,lambda 是“给定 param,忽略 param 并调用 OnRequestClose()

RelayCommand is presumably a delegate-type that accepts a single parameter, or a type that itself takes such a delegate-type in the constructor. You are declaring an anonymous method, saying simply "when invoked, we'll take the incoming value (but then not use it), and call OnRequestClose. You could also have (maybe clearer):

_closeCommand = new RelayCommand(delegate { this.OnRequestClose(); });

It is probably clearer in other uses where it is used, for example:

var ordered = qry.OrderBy(item => item.SomeValue);

where the lambda is "given an item, obtain the item's SomeValue". In your case the lambda is "given param, ignore param and call OnRequestClose()"

惟欲睡 2024-10-03 10:36:21

参数=> this.OnRequestClose() 是 lambda

Func<sometype_that_param_is,sometype_that_OnRequestClose_Is>


操作

我不确定是哪个

所以它只是一个由某些东西调用的函数的表达式,它将传递一个参数,该参数将是“param”然后不使用

param => this.OnRequestClose() is lambda

Func<sometype_that_param_is,sometype_that_OnRequestClose_Is>

or
Action

I'm not sure which

So it's just an expression for a func that is called by something, that will pass an argument which will be 'param' an then not used

荒岛晴空 2024-10-03 10:36:21

没有什么。该行定义了表示函数的 lambda 表达式。该函数具有如下签名: Foo(T param) 其中 T 将是编译器根据被调用构造函数的参数类型推断出的特定类型。

Nothing. The line defines a lambda expression representing a function. That function have a signature like: Foo(T param) where T will be a specific type infered by the compiler based on the type of the argument of the construtor being called.

ゃ懵逼小萝莉 2024-10-03 10:36:21

param 是 lambda 表达式的唯一参数 ( param=>this.OnRequestClose() )

当您实例化 ICommand 对象时,param 可能包含从 UI 传递到此 ICommand 的参数。在您的情况下,该参数未在命令中使用(它不会出现在 lambda 表达式的右侧)。

param is the only parameter of your lambda expression ( param=>this.OnRequestClose() )

As you are instantiating an ICommand object, param would probably contain the parameter passed to this ICommand from the UI. In your case, the parameter is not used in the command (it does not appear on the right side of the lambda expression).

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