Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self referencing generic type constraint and XAML in UWP application

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 !

like image 314
rolandl Avatar asked Nov 05 '15 17:11

rolandl


1 Answers

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.

like image 90
Kevin Gosse Avatar answered Nov 11 '22 10:11

Kevin Gosse