Prism DelgateComand 抛出异常

Prism DelgateComand throws an exception

我有一个静态委托命令。我正在将布尔值传递给构造函数。但是,它抛出运行时异常。

public static class myViewModel
{
    public static ICommand myCommand {get; private set;}

    static myViewModel
    {
        //If I change the bool to Object, or to Collection type, no exception assuming that I change myMethod parameter as well to the same type.
        myCommand = new DelegateCommand<bool>(myMethod);
    }

    private static void myMethod (bool myBoolean)
    {
        //To Do
    }
}

总是,总是,总是告诉我们您遇到的 type 异常是什么,以及异常 message 是什么。他们有很多很多不同的异常,因为每个异常都是出于不同的原因抛出的。

但在这种情况下,问题似乎在于 bool 是一个值类型,执行命令的代码将 null 作为参数传递给它。但是你不能将 null 转换为值类型,并且尝试这样做会导致运行时异常:

object o = null;
//  This will compile, but blow up at runtime. 
bool b = (bool)o;