UWP 应用程序中的自引用泛型类型约束和 XAML
Self referencing generic type constraint and XAML in UWP application
我目前正在开发一个 UWP 应用程序,其中我在 PCL.
中使用 self referencing generic type constraint
这里是 PCL.
的 classes 的描述
首先,实现自引用泛型类型约束的class(这个class也实现了new()
约束)
public abstract class A<T>
where T : A<T>, new()
{
//...
}
然后,我有一个基 class 扩展 Windows.UI.Xaml.Controls.Page
class :
public abstract class MyPage<T> : Page
where T : A<T>, new()
{
//...
}
我还有一个基础 class 扩展了 Windows.UI.Xaml.Application
class :
public abstract class MyApplication<T> : Application
where T : A<T>, new()
{
//...
}
我的 UWP classes 通过以下方式扩展了上面描述的 PCL 的 classes...
首先,我有一个 class B
扩展 class A
:
public sealed class B : A<B>
{
//...
}
然后,我有扩展 class MyApplication
的 class 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"
/>
然后,我有一个扩展 class MyPage
的 HomePage
。 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)
根据这些问题,我需要在classes App
和HomePage
的XAML中指定genereic类型,所以这里新增XAML 文件根据 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"
>
但是不行。我仍然有以前的错误...和新的错误:
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.
中仍然不受支持
作为解决方法,您可以在继承树中添加一个中间 class 来设置通用约束:
public abstract class BaseApp : MyApplication<B>
{
}
然后让您的应用继承它:
sealed partial class App : BaseApp
并相应地更改您的 XAML:
<local:BaseApp
x:Class="My.Project.App"
脏,但不幸的是,您无能为力。
我目前正在开发一个 UWP 应用程序,其中我在 PCL.
中使用 self referencing generic type constraint这里是 PCL.
的 classes 的描述首先,实现自引用泛型类型约束的class(这个class也实现了new()
约束)
public abstract class A<T>
where T : A<T>, new()
{
//...
}
然后,我有一个基 class 扩展 Windows.UI.Xaml.Controls.Page
class :
public abstract class MyPage<T> : Page
where T : A<T>, new()
{
//...
}
我还有一个基础 class 扩展了 Windows.UI.Xaml.Application
class :
public abstract class MyApplication<T> : Application
where T : A<T>, new()
{
//...
}
我的 UWP classes 通过以下方式扩展了上面描述的 PCL 的 classes...
首先,我有一个 class B
扩展 class A
:
public sealed class B : A<B>
{
//...
}
然后,我有扩展 class MyApplication
的 class 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"
/>
然后,我有一个扩展 class MyPage
的 HomePage
。 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)
根据这些问题,我需要在classes App
和HomePage
的XAML中指定genereic类型,所以这里新增XAML 文件根据 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"
>
但是不行。我仍然有以前的错误...和新的错误:
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.
中仍然不受支持作为解决方法,您可以在继承树中添加一个中间 class 来设置通用约束:
public abstract class BaseApp : MyApplication<B>
{
}
然后让您的应用继承它:
sealed partial class App : BaseApp
并相应地更改您的 XAML:
<local:BaseApp
x:Class="My.Project.App"
脏,但不幸的是,您无能为力。