繁体   English   中英

在UWP应用程序中自引用泛型类型约束和XAML

[英]Self referencing generic type constraint and XAML in UWP application

我目前正在开发一个UWP应用程序,其中我在PCL中使用自引用泛型类型约束

这里描述了PCL的类。

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

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

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

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

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

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

我的UWP课程以下列方式扩展了上述PCL的类...

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

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

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

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

然后,我有一个扩展类MyPageHomePage csharp文件:

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

当我尝试编译时,我有以下错误(消息是法语,我试图用英语翻译它们):

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

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

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

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

根据这些问题,我需要在AppHomePage类的XAML中指定genereic类型,所以这里是根据这篇文章的新XAML文件:

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

但它不起作用。 我仍然有以前的错误...和新的错误:

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

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

你知道如何解决这个问题吗?

预先感谢您的帮助 !

鉴于此答案(对于Windows 8)和您的结果,我认为我们可以安全地假设Windows 10中仍不支持XAML中的通用参数。

作为一种变通方法,您可以在继承树中添加一个中间类来设置泛型约束:

public abstract class BaseApp : MyApplication<B>
{        
}

然后让你的应用继承它:

sealed partial class App : BaseApp

并相应地更改您的XAML:

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

很脏,但不幸的是,你无能为力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM