c# - UWP 应用程序中的自引用泛型类型约束和 XAML

标签 c# xaml generics uwp

我目前正在开发一个 UWP 应用程序,我在其中使用了一个 self referencing generic type constraint在 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 . 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"
>

当我尝试编译时,出现以下错误(消息是法语的,我试图将它们翻译成英语):

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)

根据这些问题,我需要在类的 XAML 中指定通用类型 AppHomePage ,所以这里是根据 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, 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)

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

预先感谢您的帮助!

最佳答案

Given this answer (for Windows 8)和你的结果,我认为我们可以安全地假设 XAML 中的泛型参数在 Windows 10 中仍然不受支持。

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

public abstract class BaseApp : MyApplication<B>
{        
}

然后让您的应用继承它:

sealed partial class App : BaseApp

并相应地更改您的 XAML:

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

很脏,但不幸的是,您对此无能为力。

关于c# - UWP 应用程序中的自引用泛型类型约束和 XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33550495/

相关文章:

c# - 如何将 MainWindow.xaml 拆分为单独的文件? - 外包<Style>?

c# - 删除 TextBox 末尾的 "X"按钮

java - ( Play 2.1.3)最小化模型中的代码。静态方法的泛型

c# - Windows 8 Metro 风格列表框

c# - 基准测试 .Net 正则表达式选项 - 或者 RightToLeft 做什么?

c# - 如何从 C# 方法返回 JavaScript 'native' 数组?

c# - 我怎样才能写一个通用的匿名方法?

c# - 尝试使用 linq 查询同一文件夹中的多个文本文件

wpf - 创建六边形网格

具有泛型参数的 Swift 3 泛型扩展约束