是否可以在 XAML 中声明类型别名?
是否可以在 XAML 中声明类型的别名?
让我用一个例子来解释一下。鉴于这些类型声明...
namespace Somewhere
{
public class Blob { … }
public class BlobCollection : List<Blob> {} // "type alias" in C#
}
...以下(缩写)XAML 应该是有效的:
<BlobCollection xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:Somewhere;…">
<Blob … />
<Blob … />
</BlobCollection>
我已经知道我可以通过继承定义类似类型别名的内容(请参阅上面的代码注释)。假设想要在 XAML 中执行相同的操作,我必须如何更改 XAML 才能将 BlobCollection
引用为 Blob
?
<Blobs xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:Somewhere;…">
<Blob … />
<Blob … />
</Blobs>
Is it possible in XAML to declare an alias name for a type?
Let me explain with an example. Given these type declarations...
namespace Somewhere
{
public class Blob { … }
public class BlobCollection : List<Blob> {} // "type alias" in C#
}
... the following (abbreviated) XAML should be valid:
<BlobCollection xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:Somewhere;…">
<Blob … />
<Blob … />
</BlobCollection>
I already know that I can define something like type aliases through inheritance (see code comment above). Suppose that wanted to do the same in XAML, how would I have to change the XAML in order to be able to refer to BlobCollection
as Blobs
?
<Blobs xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:Somewhere;…">
<Blob … />
<Blob … />
</Blobs>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否可以直接在 XAML 中执行别名操作,但最简单的方法是在 XAML 中使用别名,只需在代码中对集合进行子类化即可(或者,如果不会对其余部分产生不利影响,则重命名集合类本身)你的代码):
似乎确实没有必要这样做,但这是我现在能想到的。
I'm not sure if you can do the aliasing directly in XAML, but it's easiest to use the aliased name in XAML by simply subclassing the collection in code (or heck, renaming the collection class itself if it won't adversely affect the rest of your code):
It does seem unnecessary to have to do this, but it's all I can think of right now.