2015-12-02 32 views
2

我想了解我在UWP应用程序上工作时遇到的这个问题。我能够解决这个问题,但我仍然不清楚其背后的解释/推理。UWP应用程序的调试和发布模式

我在我的代码库中使用XAMLBehaviours SDK中的“EventTriggerBehavior”。这个事件是为了检查GridView的“ClickedItem”。

Microsoft.Xaml.Interactivity的IAction.Execute方法获取ClickedItem事件作为参数。

函数定义为对象IAction.Execute(对象发件人,对象参数)

当我运行在调试模式下的应用程序,这是工作的罚款和参数是越来越分配正确的值。但是当我对Release进行配置时,我意识到我的Behaviors SDK工作不正常。

这是上面的代码片段:

object IAction.Execute(object sender, object parameter) 
    { 

     object propertyValue = parameter; 
     foreach (var propertyPathPart in propertyPathParts) 
     { 
      var propInfo = propertyValue.GetType().GetTypeInfo().GetDeclaredProperty(propertyPathPart); 
      if (propInfo != null) 
       propertyValue = propInfo.GetValue(propertyValue); 
     } 
    } 

在进一步的调查,我意识到的PropertyValue没有得到正确的值初始化。因此,为了解决这个问题,我对参数进行了类型转换。

object propertyValue = parameter as ItemClickEventArgs; 

现在一切都开始在发布模式下正常工作,包括启用代码优化时。

我将分类到该System.reflection在释放模式下工作正常,当编译.NET本地工具链已启用。当我进行隐式投射时,它不再是一个问题。

根据此视频https://channel9.msdn.com/Shows/Going+Deep/Inside-NET-Native,反射仍然有效,但我不得不投入Behaviors SDK。我想知道更详细的信息并正确理解。

回答

1

在您的项目uwp中,您可以找到名为Default.rd.xml(属性文件夹内)的文件。这是一个配置文件,用于指定启用.net本机时指定的程序元素是否可用于反射(或不可用)。

在你的情况下,你可以添加下面的声明来添加ItemClickEventArgs类型。如果需要,可以选择声明一个名称空间而不是类型。

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata"> 
    <Application> 
    <!-- 
     An Assembly element with Name="*Application*" applies to all assemblies in 
     the application package. The asterisks are not wildcards. 
    --> 
    <Assembly Name="*Application*" Dynamic="Required All"/> 

    <!-- Add your application specific runtime directives here. --> 
    <Type Name="Windows.UI.Xaml.Controls.ItemClickEventArgs" Browse="Required Public"/> 

    </Application> 
</Directives> 

您可以检查此链接了解更多详情:

Reflection and .NET Native

NET Native Deep Dive: Help! I Hit a MissingMetadataException

相关问题