Xamarin.forms: Defining generic classes in XAML

Created on 15 Jan 2018  Â·  7Comments  Â·  Source: xamarin/Xamarin.Forms

Description

It is possible to specify a class with type parameters in XAML (see step 2), but the generated (partial) definition will cause a runtime failure.

Steps to Reproduce

  1. Add <T> to parameterize a ContentPage derivative in its code-behind
  2. Add &lt;T&gt; to x:Class in its XAML
  3. Try instantiating it with a concrete type

Expected Behavior

The class is successfully instantiated according to its definitions in XAML and code-behind

Actual Behavior

Runtime failure at line global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(GenericClass<T>));

An exception of type 'Xamarin.Forms.Xaml.XamlParseException' occurred in Xamarin.Forms.Xaml.dll but was not handled in user code

Additional information: No embeddedresource found for Project.GenericClass`1[ConcreteType]

Basic Information

  • Version with issue: Xamarin.Forms.2.5.0.122203
  • Last known good version: N/A (it's the same on 2.4.0.280)
  • IDE: Visual Studio 2015
  • Platform Target Frameworks:

    • UWP: 10.0A

  • Affected Devices: All
Xaml </> enhancement âž•

All 7 comments

Defining generic classes in Xaml is not supported at this time.

step 2 should also be

Add '`1' to x:Class

I'm keeping this as a feature request

Thanks for picking it up.
As for step 2, it is <T> encoded using XML entities &lt; (less than) and &gt; (greater than), or am I missing something? Perhaps you're missing entities in your markdown (things between actual < and > get removed I've noticed)?

@developMediaSoft The docs for x:Class say it uses the same format as Type.FullName which would mean a generic class such as List<String> would be written as System.Collections.Generic.List`1[System.String]
Thus your step 2 would be `1[T] rather than &lt;T&gt;

I think there would be some issues with knowing the list of concrete types that could be instantiated - unless T was internal and all derived classes were sealed??

I have a number of XAML pages which are concrete implementations of a Generic base class which works for my situation - but I don't think that is what you are looking for. (See x:TypeArguments)

Thank you for explaining that @CZEMacLeod, I must say I've only learned this fact about x:Class from you now. My use of entities was a guess.

What would knowing concrete types be necessary for?

Indeed simply parameterizing an already-defined generic class in XAML utilizing x:TypeArguments is not what I'm after, but I appreciate you mentioning it for the sake of those who might find this thread via search engines.

I don't foresee this being implemented in the coming months by the XF team, and no one volunteered, so I'll close it. We'll reopen it if needed, or I'm sure we can come up with that exact same idea again

Our Xamarin.Forms-based project is in a similar state as this issue

So this will never be implemented, right?

Was this page helpful?
0 / 5 - 0 ratings