Well, I had this problem too, but I didn't want to include a public constructor without parameters in my class, so I found another way.
The main problem is that WinRT is a native platform and cannot reflect .NET code. Therefore, the build process for WinRT applications generates metadata about the types used in XAML (you can find the corresponding code in obj/(Debug|Release)/XamlTypeInfo.g.cs ).
If a type is never used in XAML, metadata of this type is not generated, which means (by the way) that you cannot animate properties of this type.
If you are writing a class library, you can simply turn on the XAML resource dictionary and declare a dummy type instance; this will lead to metadata generation. However, this requires the type to have an open constructor without parameters. This may be undesirable.
So, there is another solution: provide the metadata yourself. There are several interfaces to implement, and they have many members, so this can be quite tedious to do manually. Fortunately, you donβt have to! Here is what you can do:
- add an open class without parameters to the class (temporarily)
- create a XAML
ResourceDictionary and declare an instance of the class in it (as described above) - copy the
XamlTypeInfo.g.cs file to your project (I renamed it to XamlTypeInfo.cs ) - replace the constructor call with
throw new NotImplementedException() - delete the
ResourceDictionary file - remove open constructor without parameters
And you're done, the animation now works correctly.
The process is still quite tedious, so it would be nice to have a tool to do this work for us ...
EDIT: much simpler: apply the [Bindable] attribute to the class. This makes the metadata generator type-sensitive, even if it is not used in XAML. (ignore the fact that the document talks about it for C ++ types, it works great for C # classes too)
Thomas levesque
source share