6

I am currently working on a UWP application in which one I use self referencing generic type constraint in a PCL.

Here a description of the classes of the PCL.

First, the class that implements the self referencing generic type constraint (this class also implements the new() constraints)

public abstract class A<T> 
  where T : A<T>, new()
{
  //...
}

Then, I have a base class that extends the Windows.UI.Xaml.Controls.Page class :

public abstract class MyPage<T> : Page
  where T : A<T>, new()
{
  //...
}

I also have a base class that extends the Windows.UI.Xaml.Application class :

public abstract class MyApplication<T> : Application
  where T : A<T>, new()
{
  //...
}

My UWP classes extends the classes of the PCL describe above in the following way...

First, I have a class B that extends the class A :

public sealed class B : A<B>
{
  //...
}

Then, I have the class App that extends the class MyApplication. The csharp file :

public sealed partial class App : MyApplication<B>
{
  //...
}

And the XAML file :

<custom:MyApplication
  x:Class="My.Project.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:custom="using:My.PCL"
  RequestedTheme="Light"
/>

Then, I have a HomePage that extends the class MyPage. The csharp file :

public sealed partial class HomePage : MyPage<B>
{
  //...
}

And the XAML file :

<custom:MyPage
  x:Class="My.Project.HomePage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:custom="using:My.PCL"
  mc:Ignorable="d"
>

When I try to compile, I have the following errors (the messages are in french, I tried to translate them in english) :

Used of generic type MyApp<T> needs arguments of type 1 (file App.i.cs)

Used of generic type MyPage<T> needs arguments of type 1 (file HomePage.i.cs)

The name MyApp does not exist in the namespace using:My.PCL (file App.xaml)

The name MyPage does not exist in the namespace using:My.PCL (file HomePage.xaml)

According to these issues, I need to specify the genereic type into the XAML of the classes App and HomePage, so here the new XAML file according to this article :

<custom:MyApplication
  x:Class="My.Project.App"
  x:TypeArguments="local:B"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:custom="using:My.PCL"
  xmlns:local="using:My.Project"
  RequestedTheme="Light"
/>

and

<custom:MyPage
  x:Class="My.Project.HomePage"
  x:TypeArguments="local:B"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:custom="using:My.PCL"
  xmlns:local="using:My.Project"
  mc:Ignorable="d"
>

But it does not work. I still have the previous errors... and new ones :

GenericArguments[0], System.Object, on My.PCL.MyApplication'1[T] violates the constraint of type T (file App.xaml)

GenericArguments[0], System.Object, on My.PCL.MyPage'1[T] violates the constraint of type T (file HomePage.xaml)

Do you have any idea how to fix this issue ?

Thank you in advance for your help !

Community
  • 1
  • 1
rolandl
  • 1,769
  • 1
  • 25
  • 48

1 Answers1

12

Given this answer (for Windows 8) and your result, I think we can safely assume that generic parameters in XAML still aren't supported in Windows 10.

As a workaround, you can add an intermediate class in the inheritance tree to set the generic constraint:

public abstract class BaseApp : MyApplication<B>
{        
}

Then make your app inherit from it:

sealed partial class App : BaseApp

And change your XAML accordingly:

<local:BaseApp
    x:Class="My.Project.App"

Dirty, but unfortunately there isn't much you can do about it.

Community
  • 1
  • 1
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • so sad... :( Your solution works ! Thk you for your help ! – rolandl Nov 05 '15 at 20:58
  • Does Xaml for UWP support x:TypeArguments already? I still see it generates class without generic arguments in *.g.i.cs file :( – Aleksander Stankiewicz Sep 09 '16 at 14:43
  • I see although such solution works in runtime the designer can't instantiate such page based on custom type and still shows errors :( ... – Aleksander Stankiewicz Sep 09 '16 at 15:23
  • Generic arguments are not supported in UWP. Please vote [here](https://developercommunity.visualstudio.com/idea/610869/support-generic-type-arguments-in-uwp-xaml.html). – Shimmy Weitzhandler Jun 17 '19 at 20:41
  • Once you get over it, this is not a bad workaround, its 3 (or 1) lines of code into each xaml view that you _really_ want this functionality in. The XAML way to do this is through attached properties but it requires you to use a completely structurally different approach. – Chris Schaller Jun 17 '20 at 04:20