【问题标题】:Using event arguments in portable class library在可移植类库中使用事件参数
【发布时间】:2013-08-12 20:37:55
【问题描述】:

我目前正在开发适用于 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。

有什么办法可以做到吗?

【问题讨论】:

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


    【解决方案1】:

    我尝试了反射,它确实有效。

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

    【讨论】:

      【解决方案2】:

      经过我的大量尝试,我终于找到了我的解决方案。 像 user1545884 一样,我也使用反射,但是 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)
          {
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2016-07-08
        • 2013-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多