c# - 为什么 RelayCommand<T>.Execute 采用对象而不是 T?

标签 c# silverlight mvvm relaycommand

除了导致需要进行不必要的转换之外,这没有做任何事情(或者更确切地说,导致我拉下代码库并自己进行更改)。这样做有什么理由吗?

引用资料:

Source on Codeplex

Blog posting with source

编辑 这是一个例子:

DoCommand = new RelayCommand<AsyncCallback>((callBack) =>
{
    Console.WriteLine("In the Action<AsyncCallback>");
    SomeAsyncFunction((async_result) =>
    {
        Console.WriteLine("In the AsyncCallback");
        callBack.Invoke(new MyAsyncResult(true));
    });
});

DoCommand.Execute((iasyncresult) => Console.WriteLine(iasyncresult.IsCompleted));
//Where MyAsyncResult is a class implement IAsyncResult that sets IsCompleted in the constructor
// This will cause the "cannot cast lambda as object" error

最佳答案

因为 ICommand不是通用的。 ICommand 的通用实现必须从接口(interface)转换,处理无效转换,并将转换实例转发给通用方法。

关于c# - 为什么 RelayCommand<T>.Execute 采用对象而不是 T?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6296396/

相关文章:

c# - 是否可以使用 C# 在 ArrayList 中存储多种数据类型?

c# - WPF 列表框与组合框

mvvm - 无法建立新建立的项目(Hamburger范本)

silverlight - 传递更新到 ViewModel 的 DependencyProperty 是一种好习惯吗?

c# - 当一个为空时比较整数和默认比较为真

c# - 带有数字键的动态 json 对象

c# - 在策略上使用多个身份验证方案会导致签名验证失败

c# - ASP.net Core API 中的路由

c# - 带有 asp.net mvc 3 的 jquery 网络摄像头插件

c# - 我可以在所有用户使用的 silverlight 中缓存数据吗?