UWP 应用的调试和发布模式

Debug and Release Mode of UWP Apps

我正在尝试了解我在处理 UWP 应用程序时遇到的这个问题。我能够解决这个问题,但我仍然不清楚它背后的 explanation/reasoning。

我在我的代码库中使用来自 XAMLBehaviours SDK 的 "EventTriggerBehavior"。此事件用于检查 GridView 的 "ClickedItem"。

IAction.Execute 来自 Microsoft.Xaml.Interactivity 的方法获取 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 Native 工具链编译时在发布模式下工作不正常。当我进行隐式转换时,这不再是问题了。

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

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

根据您的情况,您可以添加以下声明来添加 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