簡體   English   中英

為什么 RelayCommand<t> .Execute 使用 object 而不是 T?</t>

[英]Why does RelayCommand<T>.Execute take an object instead of a T?

這並沒有做任何事情,只是導致需要進行不必要的強制轉換(或者更確切地說,導致我拉下代碼庫並自己進行更改)。 這樣做有理由嗎?

參考:

Codeplex 上的源代碼

帶有來源的博客發布

編輯這是一個例子:

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的通用實現必須從接口進行轉換,處理無效轉換,並將轉換實例轉發給通用方法。

您的錯誤是由於 lambda 無法作為object傳遞。 而是嘗試:

AsyncCallback callback = (iasyncresult) => Console.WriteLine(iasyncresult.IsCompleted);
DoCommand.Execute(callback);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM