在可移植类库中使用事件参数

时间:2013-07-04 08:30:21

标签: xaml windows-8 windows-phone-8 portability eventargs

我目前正在为Windows 8和Windows Phone 8开发应用程序。视图的构建必须非常动态,因此我必须使用大量的数据表。在这些数据模板中,我使用Galasoft MVVMLight EventToCommand,因为我需要事件参数,我还使用PassEventArgsToCommand =" True"。

<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding Path=SelectionChangedCommand}" 
                        PassEventArgsToCommand="True"/>
</i:EventTrigger>

问题是,我需要使用这些事件参数的类(我的命令发生的地方)必须在可移植的类库中,因此我不能在这些类中使用特定于平台的事件参数。

private void SelectionChanged(SelectionChangedEventArgs sel)
{
     //do something
}

在我的情况下,我必须使用SelectionChangedEventArgs,DateTimeValueChangedEventArgs和KeyEventArgs。

有没有办法让这项工作?

2 个答案:

答案 0 :(得分:0)

我尝试过反思并且确实有效。

string keyCode = "";

        if (args != null)
        {
            PropertyInfo p = args.GetType().GetProperty("Key");
            keyCode = p.GetValue(args, null).ToString();
        }

答案 1 :(得分:0)

经过多次尝试后,我终于找到了解决方案。 和user1545884一样,我也使用Reflection,但GetValue方法对我不起作用 SelectedItem对象来自“AddedItems”集合,因此我使用“AddedItems”获取GetProperty方法 我直到找到了我的对象。

IEnumerable ie = (parameter.GetType().GetRuntimeProperty("AddedItems").GetValue(parameter) as IEnumerable);
foreach (var item in ie)
{
    if (item is Store_Province)
    {

    }
}
相关问题