Example: Handling Exceptions When Binding Data
The following example shows how to resolve a MissingMetadataException exception that is thrown when an app compiled with the .NET Native tool chain tries to bind data. Here's the exception information:
This operation cannot be carried out as metadata for the following type was removed for performance reasons:
App.ViewModels.MainPageVM
Here's the associated call stack:
Reflection::Execution::ReflectionDomainSetupImplementation.CreateNonInvokabilityException+0x238
Reflection::Core::ReflectionDomain.CreateNonInvokabilityException+0x2e
Reflection::Core::Execution::ExecutionEnvironment.+0x316
System::Reflection::Runtime::PropertyInfos::RuntimePropertyInfo.GetValue+0x1cb
System::Reflection::PropertyInfo.GetValue+0x22
System::Runtime::InteropServices::WindowsRuntime::CustomPropertyImpl.GetValue+0x42
App!$66_Interop::McgNative.Func_IInspectable_IInspectable+0x158
App!$66_Interop::McgNative::__vtable_Windows_UI_Xaml_Data__ICustomProperty.GetValue__STUB+0x46
Windows_UI_Xaml!DirectUI::PropertyProviderPropertyAccess::GetValue+0x3f
Windows_UI_Xaml!DirectUI::PropertyAccessPathStep::GetValue+0x31
Windows_UI_Xaml!DirectUI::PropertyPathListener::ConnectPathStep+0x113
What was the app doing?
At the base of the stack, frames from the Windows.UI.Xaml namespace indicate that the XAML rendering engine was running. The use of the PropertyInfo.GetValue method indicates a reflection-based lookup of a property's value on the type whose metadata was removed.
The first step in providing a metadata directive would be to add serialize
metadata for the type so that its properties are all accessible:
<Type Name="App.ViewModels.MainPageVM" Serialize="Required Public" />
Is this an isolated case?
In this scenario, if data binding has incomplete metadata for one ViewModel
, it may for others, too. If the code is structured in a way that the app's view models are all in the App.ViewModels
namespace, you could use a more general runtime directive:
<Namespace Name="App.ViewModels " Serialize="Required Public" />
Could the code be rewritten to not use reflection?
Because data binding is reflection-intensive, changing the code to avoid reflection isn't feasible.
However, there are ways to specify the ViewModel
to the XAML page so that the tool chain can associate property bindings with the correct type at compile time and keep the metadata without using a runtime directive. For example, you could apply the Windows.UI.Xaml.Data.BindableAttribute attribute on properties. This causes the XAML compiler to generate the required lookup information and avoids requiring a runtime directive in the Default.rd.xml file.