我目前正在开发一个 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 namespaceusing:My.PCL
(file App.xaml)The name
MyPage
does not exist in the namespaceusing:My.PCL
(file HomePage.xaml)
根据这些问题,我需要在类的 XAML 中指定通用类型 App
和 HomePage
,所以这里是根据 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
, onMy.PCL.MyApplication'1[T]
violates the constraint of typeT
(file App.xaml)GenericArguments[0],
System.Object
, onMy.PCL.MyPage'1[T]
violates the constraint of typeT
(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/