3
votes

I am trying to understand this one issue which I faced when I was working on UWP applications. I was able to fix this issue but I am still not clear with the explanation/reasoning behind it.

I am using "EventTriggerBehavior" from XAMLBehaviours SDK in my codebase. This event was to check for "ClickedItem" of GridView.

IAction.Execute Method from Microsoft.Xaml.Interactivity gets the ClickedItem event as parameter.

The function definition is object IAction.Execute( object sender, object parameter )

When I was running the app in Debug mode,this was working fine and parameter was getting the correct values assigned. But when I did my configuration to Release, I realized that my Behaviors SDK is not working correctly.

This was the previous code snippet:

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);
        }
    }

On further investigation, I realized that propertyValue does not get initialized with the correct value. Hence, to fix this, I did Type Cast on parameter.

object propertyValue = parameter as ItemClickEventArgs;

Now everything started working fine in Release Mode including when code optimization is enabled.

I would classify into that System.reflection was not working fine in Release mode when Compile with .NET Native tool chain was enabled. When I did Implicit cast, it wasnt a problem anymore.

According to this video https://channel9.msdn.com/Shows/Going+Deep/Inside-NET-Native, Reflection still works yet I had to cast for Behaviors SDK. I would like to know more detailed info for this and understand this properly.

1

1 Answers

3
votes

In your project uwp, you can find a file named Default.rd.xml (inside Properties folder). This is a configuration file that specifies whether designated program elements are available for reflection (or not) when .net native is enabled.

In your situation, you can add the following declaration to add ItemClickEventArgs type. There is some option to declare a namespace instead of a type if needed.

<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>

You can check this links for additional details:

Reflection and .NET Native

NET Native Deep Dive: Help! I Hit a MissingMetadataException