自引用的泛型类型约束和 UWP 应用中的 XAML

6
我是一名有用的助手,可以为您翻译文本。

我目前正在开发一个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类的功能...
首先,我有一个类B,它扩展了类A:
public sealed class B : A<B>
{
  //...
}

接下来,我有一个类App,它继承了MyApplication类。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"
/>

然后,我有一个HomePage类,它扩展了MyPage类。C#文件:

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 中指定泛型类型,因此根据 this article 的新 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"
>

但是它没有起作用。我仍然有之前的错误...和新的错误:
GenericArguments[0],System.Object,在My.PCL.MyApplication'1[T]上违反了类型T的约束(文件App.xaml)
GenericArguments[0],System.Object,在My.PCL.MyPage'1[T]上违反了类型T的约束(文件HomePage.xaml)
你有任何解决这个问题的想法吗?
提前感谢您的帮助!

与XAML无关。public sealed partial class HomePage:MyPage<A> 您不能在没有泛型约束的情况下使用A,必须是A<Something> - Kevin Gosse
@KooKiz:抱歉,我试图匿名化该项目并犯了一个错误...我已经编辑了我的消息...它是public sealed partial class HomePage:MyPage<B>public sealed partial class App:MyApplication<B> - rolandl
1个回答

12

参考这个答案(针对Windows 8)和你的结果,我认为我们可以安全地假设在Windows 10中仍不支持XAML中的泛型参数。

作为一种解决方法,您可以添加一个中间类到继承树中以设置泛型约束:

public abstract class BaseApp : MyApplication<B>
{        
}

然后让你的应用从它继承:

sealed partial class App : BaseApp

并相应更改您的XAML:

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

脏了,但不幸的是你没什么办法。


好伤心... :( 你的解决方案有效!谢谢你的帮助! - rolandl
Xaml for UWP已经支持x:TypeArguments了吗?我仍然看到它在*.g.i.cs文件中生成没有泛型参数的类:( - Aleksander Stankiewicz
我看到尽管这种解决方案在运行时可以工作,但设计师无法基于自定义类型实例化此页面,仍然会显示错误 :( ... - Aleksander Stankiewicz
UWP不支持通用类型参数。请在此处投票链接 - Shimmy Weitzhandler
一旦你克服了它,这并不是一个糟糕的解决方法,只需要在每个XAML视图中添加3(或1)行代码,以便在真正需要此功能时使用。XAML实现此功能的方式是通过附加属性,但它需要您使用完全结构不同的方法。 - Chris Schaller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接