UWP应用程序中的自引用泛型类型约束和XAML [英] Self referencing generic type constraint and XAML in UWP application

查看:82
本文介绍了UWP应用程序中的自引用泛型类型约束和XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个UWP应用程序,其中使用了

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

这里是PCL类别的描述.

Here a description of the classes of the PCL.

首先,实现自引用泛型类型约束的类(该类还实现new()约束)

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()
{
  //...
}

然后,我有一个扩展Windows.UI.Xaml.Controls.Page类的基类:

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()
{
  //...
}

我还有一个扩展Windows.UI.Xaml.Application类的基类:

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()
{
  //...
}

我的UWP类通过以下方式扩展了上面描述的PCL类...

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

首先,我有一个扩展类A的类B:

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

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

然后,我有了扩展了类MyApplication的类App. csharp文件:

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

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

还有XAML文件:

<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"
/>

然后,我有一个HomePage扩展了类MyPage. csharp文件:

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

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

还有XAML文件:

<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) :

使用的通用类型MyApp<T>需要类型1的参数(文件App.i.cs)

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

使用的通用类型MyPage<T>需要类型1的参数(文件HomePage.i.cs)

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

名称MyApp在名称空间using:My.PCL(文件App.xaml)中不存在

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

名称MyPage在名称空间using:My.PCL(文件HomePage.xaml)中不存在

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

根据这些问题,我需要在类AppHomePage的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"
/>

<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 :

My.PCL.MyApplication'1[T]上的GenericArguments [0],System.Object违反了类型T(文件App.xaml)的约束

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

My.PCL.MyPage'1[T]上的GenericArguments [0],System.Object违反了类型T(文件HomePage.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 ?

在此先感谢您的帮助!

推荐答案

给出答案(对于Windows 8)和您的结果,我认为我们可以放心地假设Windows 10中仍不支持XAML中的通用参数.

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

并相应地更改您的XAML:

And change your XAML accordingly:

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

肮脏,但不幸的是,您对此无能为力.

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

这篇关于UWP应用程序中的自引用泛型类型约束和XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆