如何将 Nullable<Boolean> 值传递给 CommandParameter?
How do I pass Nullable<Boolean> value to CommandParameter?
我正在使用 MVVM Light 库。在这个库中,我使用 RelayCommand<T>
来定义带有 T
类型参数的命令。
现在我已经定义了一个 RelayCommand
,它需要一个 Nullable<bool>
:
类型的参数
private RelayCommand<bool?> _cmdSomeCommand;
public RelayCommand<bool?> CmdSomeCommand
{
get
{
if (_cmdSomeCommand == null)
{ _cmdSomeCommand = new RelayCommand<bool?>(new Action<bool?>((val) => { /* do work */ })); }
return _cmdSomeCommand;
}
}
如何从我的 XAML 代码分配 CommandParameter?
我试图传递一个布尔值,但这会导致以下异常:
System.InvalidCastException: Invalid cast from 'System.Boolean' to 'System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
我也试过定义包含布尔值的静态属性?值并从 XAML:
引用它们
public static class BooleanHelper
{
public static bool True { get { return true; } }
public static bool False { get { return false; } }
public static bool? NullableTrue { get { return true; } }
public static bool? NullableFalse { get { return false; } }
}
XAML:
<Button Command="{Binding CmdSomeCommand}" CommandParameter="{x:Static my:BooleanHelper.NullableTrue}" />
但这会导致抛出相同的异常。我也尝试过 return new Nullable<bool>(true)
,但正如预期的那样,结果相同。
看起来 MVVM Light 在 Execute
和 CanExecute
之间以不同方式处理参数并且没有以正确的方式处理可为空的参数。
查看 https://github.com/paulcbetts/mvvmlight/blob/master/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(NET35)/Command/RelayCommandGeneric.cs#L198 第 198-205 行的代码
那里发生的事情会导致您的异常,并且可以使用以下代码轻松重现:
object t1 = (bool?)true;
// issue: for Nullable<T>, GetType will return T
if (t1.GetType() != typeof(bool?))
{
if (t1 is IConvertible)
{
var val = Convert.ChangeType(t1, typeof(bool?), null);
}
}
我怀疑你只能提交错误报告,因为你可以完美地将可空作为参数传递,它处理有错误。
我正在使用 MVVM Light 库。在这个库中,我使用 RelayCommand<T>
来定义带有 T
类型参数的命令。
现在我已经定义了一个 RelayCommand
,它需要一个 Nullable<bool>
:
private RelayCommand<bool?> _cmdSomeCommand;
public RelayCommand<bool?> CmdSomeCommand
{
get
{
if (_cmdSomeCommand == null)
{ _cmdSomeCommand = new RelayCommand<bool?>(new Action<bool?>((val) => { /* do work */ })); }
return _cmdSomeCommand;
}
}
如何从我的 XAML 代码分配 CommandParameter?
我试图传递一个布尔值,但这会导致以下异常:
System.InvalidCastException: Invalid cast from 'System.Boolean' to 'System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
我也试过定义包含布尔值的静态属性?值并从 XAML:
引用它们public static class BooleanHelper
{
public static bool True { get { return true; } }
public static bool False { get { return false; } }
public static bool? NullableTrue { get { return true; } }
public static bool? NullableFalse { get { return false; } }
}
XAML:
<Button Command="{Binding CmdSomeCommand}" CommandParameter="{x:Static my:BooleanHelper.NullableTrue}" />
但这会导致抛出相同的异常。我也尝试过 return new Nullable<bool>(true)
,但正如预期的那样,结果相同。
看起来 MVVM Light 在 Execute
和 CanExecute
之间以不同方式处理参数并且没有以正确的方式处理可为空的参数。
查看 https://github.com/paulcbetts/mvvmlight/blob/master/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(NET35)/Command/RelayCommandGeneric.cs#L198 第 198-205 行的代码
那里发生的事情会导致您的异常,并且可以使用以下代码轻松重现:
object t1 = (bool?)true;
// issue: for Nullable<T>, GetType will return T
if (t1.GetType() != typeof(bool?))
{
if (t1 is IConvertible)
{
var val = Convert.ChangeType(t1, typeof(bool?), null);
}
}
我怀疑你只能提交错误报告,因为你可以完美地将可空作为参数传递,它处理有错误。