2015-12-02 73 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

相關問題