问题描述
我想传递一个枚举值作为CommandParameter。
我的枚举定义为:
公共枚举MyEnum
{
一,
二
}
在我AXML我有:
地方:MvxBind =点击MyCommand,CommandParameter = MyEnum.One
...
本地:MvxBind =点击MyCommand,CommandParameter = MyEnum.Two
和MyCommand在我的ViewModel定义为
公共IMvxCommand MyCommand
{
{返回新MvxCommand< MyEnum>(MyFunction的); }
}私人无效MyFunction的(MyEnum p_enumParam)
{
开关(p_enumParam)
{
案例MyEnum.One:
doSomething1();
打破;
案例MyEnum.Two:
了doSomething2();
打破;
}
}
当我运行它,我得到的错误System.InvalidCastException:不能从源类型到目标类型强制转换
显然,因为它不能施放 谢谢, MvvmCross不能从绑定声明猜枚举类型 - 所以它不能执行此结合 在这个最简单的路线可能是解决这一使用 另一种方法是,您还可以实现一个枚举解析ValueConverter刚刚解析字符串 - 例如你可以立足于https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/ValueConverters/MvxCommandParameterValueConverter.cs - 您可以添加 MyEnum.One
和 MyEnum.Two
来的MyEnum类型。所以,我怎么能说服它的 MyEnum.One
和 MyEnum.Two
是 MyEnum 键入
巴氏字符串
!而非 - 然后你就需要使用 Enum.Parse
从字符串到您的视图模型的枚举。 Enum.Parse
来此创建:
公共类MyEnumCommandValueConverter
:MvxValueConverter< ICommand的,ICommand的>
{
保护覆盖的ICommand转换(ICommand的价值,System.Type的TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
返回新MvxWrappingCommand(值,Enum.Parse(typeof运算(MyEnum),parameter.ToString()));
}
}
然后,您可以使用绑定嵌套 - 使用类似:
地方:MvxBind =点击MyEnumCommand(MyCommand,'两')
I want to pass an enum value as a CommandParameter.My enum is defined as:
public enum MyEnum
{
One,
Two
}
And in my axml I have:
local:MvxBind="Click MyCommand, CommandParameter=MyEnum.One"
...
local:MvxBind="Click MyCommand, CommandParameter=MyEnum.Two"
and MyCommand is defined in my ViewModel as
public IMvxCommand MyCommand
{
get { return new MvxCommand<MyEnum>(myfunction); }
}
private void myfunction(MyEnum p_enumParam)
{
switch (p_enumParam)
{
case MyEnum.One:
doSomething1();
break;
case MyEnum.Two:
doSomething2();
break;
}
}
When I run it, I get the error "System.InvalidCastException: Cannot cast from source type to destination type."
Obviously, because it cannot cast MyEnum.One
and MyEnum.Two
to the MyEnum type. So how can I convince it that MyEnum.One
and MyEnum.Two
are of MyEnum
type?
Thanks,Pap
MvvmCross can't guess the type of enum from the binding statement - so it can't perform this binding.
The easiest route on this is probably to workaround this using string
s instead - and you will then need to use Enum.Parse
from the string to the enum in your ViewModel.
An alternative is that you could also implement an enum parsing ValueConverter which just parsed the string - e.g. you could base on https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/ValueConverters/MvxCommandParameterValueConverter.cs - you could add Enum.Parse
to this to create:
public class MyEnumCommandValueConverter
: MvxValueConverter<ICommand, ICommand>
{
protected override ICommand Convert(ICommand value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new MvxWrappingCommand(value, Enum.Parse(typeof(MyEnum), parameter.ToString()));
}
}
You could then bind using nesting - using something like:
local:MvxBind="Click MyEnumCommand(MyCommand, 'Two')"
这篇关于MVVCross:传递一个枚举值作为Android的一个CommandParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!